All the initialized global/static variables will go to initialized data section. All the uninitialized global/static variables will go to uninitialed data section(BSS). The variables in BSS will get a value 0 during program load time.
If a global variable is explicitly initialized to zero (int myglobal = 0
), where that variable will be stored?
Unix-like systems and Windows initialize the bss section to zero, allowing C and C++ statically allocated variables initialized to values represented with all bits zero to be put in the bss segment.
Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.
In C language both the global and static variables must be initialized with constant values. This is because the values of these variables must be known before the execution starts. An error will be generated if the constant values are not provided for global and static variables.
3) Static variables (like global variables) are initialized as 0 if not initialized explicitly. For example in the below program, value of x is printed as 0, while value of y is something garbage.
Compiler is free to put such variable into bss
as well as into data
. For example, GCC has a special option controlling such behavior:
-fno-zero-initialized-in-bss
If the target supports a BSS section, GCC by default puts variables that are initialized to zero into BSS. This can save space in the resulting code. This option turns off this behavior because some programs explicitly rely on variables going to the data section. E.g., so that the resulting executable can find the beginning of that section and/or make assumptions based on that.
The default is
-fzero-initialized-in-bss
.
Tried with the following example (test.c
file):
int put_me_somewhere = 0; int main(int argc, char* argv[]) { return 0; }
Compiling with no options (implicitly -fzero-initialized-in-bss
):
$ touch test.c && make test && objdump -x test | grep put_me_somewhere cc test.c -o test 0000000000601028 g O .bss 0000000000000004 put_me_somewhere
Compiling with -fno-zero-initialized-in-bss
option:
$ touch test.c && make test CFLAGS=-fno-zero-initialized-in-bss && objdump -x test | grep put_me_somewhere cc -fno-zero-initialized-in-bss test.c -o test 0000000000601018 g O .data 0000000000000004 put_me_somewhere
It's easy enough to test for a specific compiler:
$ cat bss.c int global_no_value; int global_initialized = 0; int main(int argc, char* argv[]) { return 0; } $ make bss cc bss.c -o bss $ readelf -s bss | grep global_ 32: 0000000000400420 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux 40: 0000000000400570 0 FUNC LOCAL DEFAULT 13 __do_global_ctors_aux 55: 0000000000601028 4 OBJECT GLOBAL DEFAULT 25 global_initialized 60: 000000000060102c 4 OBJECT GLOBAL DEFAULT 25 global_no_value
We're looking for the location of 0000000000601028
and 000000000060102c
:
$ readelf -S bss There are 30 section headers, starting at offset 0x1170: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align ... [24] .data PROGBITS 0000000000601008 00001008 0000000000000010 0000000000000000 WA 0 0 8 [25] .bss NOBITS 0000000000601018 00001018 0000000000000018 0000000000000000 WA 0 0 8
It looks like both values are stored in the .bss
section on my system: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
.
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