I know that the BSS segment stores the uninitialized global and static variables and initializes them to zero. But what if the global/static variable is initialized and my second question is I read that BSS segment doesn't consume memory, then where those it store these variables? Thanks
A data segment is a portion of the virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. Note that, the data segment is not read-only, since the values of the variables can be altered at run time.
All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).
bss section is used by the compiler for global and static variables. It is one of the default COFF sections that is used to reserve a specified amount of space in the memory map that can later be used for storing data. It is normally uninitialized.
In computing, a code segment, also known as a text segment or simply as text, is a portion of an object file or the corresponding section of the program's virtual address space that contains executable instructions.
You probably read that the BSS segment doesn't consume space in the executable file on disk. When the executable loaded, the BSS segment certainly does consume space in memory. Space is allocated and initialised to zero by the OS loader.
If initialized, global/static variables are stored in the .DATA segment. When you declare data in the .DATA segment, you provide the values to that data so it would have to be stored as part of the executable.
On the other hand, you only declare how much data you need for the .BSS since you don't need to know what the values are. So if your program declared 2 GB of uninitialized memory, that 2 GB does not contribute to the size of your executable, you won't see it until after it is loaded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With