if a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Computer Systems: A Programmer's Perspective says about two sections in a ELF relocatable object file:




  • .data Initialized global and static C variables. Local C variables are maintained at run time on the stack and do not appear in
    either the .data or .bss sections.


  • .bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero. This section
    occupies no actual space in the object file; it is merely a
    placeholder. Object file formats distinguish between initialized and
    uninitialized variables for space efficiency: unini- tialized variables
    do not have to occupy any actual disk space in the object file. At run
    time, these variables are allocated in memory with an initial value of
    zero.




Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?



if a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?



Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?



The requirements for .data and for .bss are not mutually exclusive.
The .data requirement of "Initialized global and static C variables" doesn't say that such variables must be initialized to nonzero. So if a global or static C variable is initialized to zero, should it be in .data or .bss section?



Thanks.










share|improve this question























  • Why don't you write a small C program, and test what happens?
    – dirkt
    4 hours ago






  • 1




    It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss.
    – mosvy
    4 hours ago










  • @mosvy .data is only for Initialized global and static C variables initialized to nonzero?
    – Tim
    4 hours ago











  • that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));.
    – mosvy
    1 hour ago














up vote
1
down vote

favorite












Computer Systems: A Programmer's Perspective says about two sections in a ELF relocatable object file:




  • .data Initialized global and static C variables. Local C variables are maintained at run time on the stack and do not appear in
    either the .data or .bss sections.


  • .bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero. This section
    occupies no actual space in the object file; it is merely a
    placeholder. Object file formats distinguish between initialized and
    uninitialized variables for space efficiency: unini- tialized variables
    do not have to occupy any actual disk space in the object file. At run
    time, these variables are allocated in memory with an initial value of
    zero.




Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?



if a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?



Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?



The requirements for .data and for .bss are not mutually exclusive.
The .data requirement of "Initialized global and static C variables" doesn't say that such variables must be initialized to nonzero. So if a global or static C variable is initialized to zero, should it be in .data or .bss section?



Thanks.










share|improve this question























  • Why don't you write a small C program, and test what happens?
    – dirkt
    4 hours ago






  • 1




    It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss.
    – mosvy
    4 hours ago










  • @mosvy .data is only for Initialized global and static C variables initialized to nonzero?
    – Tim
    4 hours ago











  • that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));.
    – mosvy
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Computer Systems: A Programmer's Perspective says about two sections in a ELF relocatable object file:




  • .data Initialized global and static C variables. Local C variables are maintained at run time on the stack and do not appear in
    either the .data or .bss sections.


  • .bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero. This section
    occupies no actual space in the object file; it is merely a
    placeholder. Object file formats distinguish between initialized and
    uninitialized variables for space efficiency: unini- tialized variables
    do not have to occupy any actual disk space in the object file. At run
    time, these variables are allocated in memory with an initial value of
    zero.




Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?



if a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?



Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?



The requirements for .data and for .bss are not mutually exclusive.
The .data requirement of "Initialized global and static C variables" doesn't say that such variables must be initialized to nonzero. So if a global or static C variable is initialized to zero, should it be in .data or .bss section?



Thanks.










share|improve this question















Computer Systems: A Programmer's Perspective says about two sections in a ELF relocatable object file:




  • .data Initialized global and static C variables. Local C variables are maintained at run time on the stack and do not appear in
    either the .data or .bss sections.


  • .bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero. This section
    occupies no actual space in the object file; it is merely a
    placeholder. Object file formats distinguish between initialized and
    uninitialized variables for space efficiency: unini- tialized variables
    do not have to occupy any actual disk space in the object file. At run
    time, these variables are allocated in memory with an initial value of
    zero.




Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?



if a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?



Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?



The requirements for .data and for .bss are not mutually exclusive.
The .data requirement of "Initialized global and static C variables" doesn't say that such variables must be initialized to nonzero. So if a global or static C variable is initialized to zero, should it be in .data or .bss section?



Thanks.







elf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 hours ago

























asked 4 hours ago









Tim

24.2k69233424




24.2k69233424











  • Why don't you write a small C program, and test what happens?
    – dirkt
    4 hours ago






  • 1




    It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss.
    – mosvy
    4 hours ago










  • @mosvy .data is only for Initialized global and static C variables initialized to nonzero?
    – Tim
    4 hours ago











  • that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));.
    – mosvy
    1 hour ago
















  • Why don't you write a small C program, and test what happens?
    – dirkt
    4 hours ago






  • 1




    It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss.
    – mosvy
    4 hours ago










  • @mosvy .data is only for Initialized global and static C variables initialized to nonzero?
    – Tim
    4 hours ago











  • that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));.
    – mosvy
    1 hour ago















Why don't you write a small C program, and test what happens?
– dirkt
4 hours ago




Why don't you write a small C program, and test what happens?
– dirkt
4 hours ago




1




1




It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss.
– mosvy
4 hours ago




It doesn't matter if it's "implicitly" or "explicitly" initialized -- If it starts out as 0, it's in the bss.
– mosvy
4 hours ago












@mosvy .data is only for Initialized global and static C variables initialized to nonzero?
– Tim
4 hours ago





@mosvy .data is only for Initialized global and static C variables initialized to nonzero?
– Tim
4 hours ago













that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));.
– mosvy
1 hour ago




that's the way it is by default. but you can force a variable into the .data section with eg. int var __attribute((section (".data")));.
– mosvy
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
2
down vote














Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?




“Initialised” means that the variable is given an explicit value (which might be zero) alongside its declaration in the code. “Uninitialised” means that the variable isn’t explicitly initialised at declaration time, which in your terminology corresponds to “implicitly initialised”; if it’s a global variable, its value is zero.




If a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables




Next:




Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?




Uninitialised global variables are zero-valued by default.




So if a global or static C variable is initialized to zero, should it be in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero.




Note that none of this is a requirement; in particular, it assumes that uninitialised data segments are initialised to values corresponding to zero in C on the platform. This is typically the case on systems using ELF, but C allows zero to be represented by a bit pattern other than all zeroes.



In practice however, global or static variables initialised to zero, explicitly or not, are part of .bss in ELF binaries.






share|improve this answer






















  • Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
    – Tim
    3 hours ago










  • Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
    – Stephen Kitt
    3 hours ago











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476895%2fif-a-global-or-static-c-variable-is-not-explicitly-initialized-but-implicitly-i%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote














Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?




“Initialised” means that the variable is given an explicit value (which might be zero) alongside its declaration in the code. “Uninitialised” means that the variable isn’t explicitly initialised at declaration time, which in your terminology corresponds to “implicitly initialised”; if it’s a global variable, its value is zero.




If a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables




Next:




Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?




Uninitialised global variables are zero-valued by default.




So if a global or static C variable is initialized to zero, should it be in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero.




Note that none of this is a requirement; in particular, it assumes that uninitialised data segments are initialised to values corresponding to zero in C on the platform. This is typically the case on systems using ELF, but C allows zero to be represented by a bit pattern other than all zeroes.



In practice however, global or static variables initialised to zero, explicitly or not, are part of .bss in ELF binaries.






share|improve this answer






















  • Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
    – Tim
    3 hours ago










  • Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
    – Stephen Kitt
    3 hours ago















up vote
2
down vote














Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?




“Initialised” means that the variable is given an explicit value (which might be zero) alongside its declaration in the code. “Uninitialised” means that the variable isn’t explicitly initialised at declaration time, which in your terminology corresponds to “implicitly initialised”; if it’s a global variable, its value is zero.




If a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables




Next:




Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?




Uninitialised global variables are zero-valued by default.




So if a global or static C variable is initialized to zero, should it be in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero.




Note that none of this is a requirement; in particular, it assumes that uninitialised data segments are initialised to values corresponding to zero in C on the platform. This is typically the case on systems using ELF, but C allows zero to be represented by a bit pattern other than all zeroes.



In practice however, global or static variables initialised to zero, explicitly or not, are part of .bss in ELF binaries.






share|improve this answer






















  • Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
    – Tim
    3 hours ago










  • Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
    – Stephen Kitt
    3 hours ago













up vote
2
down vote










up vote
2
down vote










Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?




“Initialised” means that the variable is given an explicit value (which might be zero) alongside its declaration in the code. “Uninitialised” means that the variable isn’t explicitly initialised at declaration time, which in your terminology corresponds to “implicitly initialised”; if it’s a global variable, its value is zero.




If a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables




Next:




Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?




Uninitialised global variables are zero-valued by default.




So if a global or static C variable is initialized to zero, should it be in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero.




Note that none of this is a requirement; in particular, it assumes that uninitialised data segments are initialised to values corresponding to zero in C on the platform. This is typically the case on systems using ELF, but C allows zero to be represented by a bit pattern other than all zeroes.



In practice however, global or static variables initialised to zero, explicitly or not, are part of .bss in ELF binaries.






share|improve this answer















Do "initialized" and "unitiailized" in the quote mean explicitly or implicitly or either?




“Initialised” means that the variable is given an explicit value (which might be zero) alongside its declaration in the code. “Uninitialised” means that the variable isn’t explicitly initialised at declaration time, which in your terminology corresponds to “implicitly initialised”; if it’s a global variable, its value is zero.




If a global or static C variable is not explicitly initialized, but implicitly initialized, is it in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables




Next:




Does it matter whether the global or static C variable is implicitly initialized to zero or nonzero?




Uninitialised global variables are zero-valued by default.




So if a global or static C variable is initialized to zero, should it be in .data or .bss section?




As per your quote:




.bss Uninitialized global and static C variables, along with any global or static variables that are initialized to zero.




Note that none of this is a requirement; in particular, it assumes that uninitialised data segments are initialised to values corresponding to zero in C on the platform. This is typically the case on systems using ELF, but C allows zero to be represented by a bit pattern other than all zeroes.



In practice however, global or static variables initialised to zero, explicitly or not, are part of .bss in ELF binaries.







share|improve this answer














share|improve this answer



share|improve this answer








edited 3 hours ago

























answered 3 hours ago









Stephen Kitt

152k23337405




152k23337405











  • Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
    – Tim
    3 hours ago










  • Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
    – Stephen Kitt
    3 hours ago

















  • Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
    – Tim
    3 hours ago










  • Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
    – Stephen Kitt
    3 hours ago
















Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
– Tim
3 hours ago




Thanks. Is .data only for Initialized global and static C variables initialized to nonzero?
– Tim
3 hours ago












Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
– Stephen Kitt
3 hours ago





Yes, your quote itself says “any global or static variables that are initialized to zero” go in .bss, not .data. See the Wikipedia page on .bss for more background.
– Stephen Kitt
3 hours ago


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476895%2fif-a-global-or-static-c-variable-is-not-explicitly-initialized-but-implicitly-i%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke