Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where do uninitialized Global Variables go after initializing?

Tags:

c

linker

elf

I struck a little problem when learning. I know that uninitialized global variables in C are assigned to the .bss section in the executable ELF file. But what happens to them when I start to use them? I.e. do they get a place on the heap or somewhere else?

I tried to find out by printing the address of the (still uninitialized) global variable with

printf("%x",&glbl);

which always return the same value 0x80495bc... Why?

like image 998
Patrick Avatar asked Aug 11 '09 13:08

Patrick


1 Answers

When the OS loads your program, it allocates enough storage from your program's address space to store everything in the .bss section and zeros all of that memory. When you assign or read from or take the address of the variable, you're manipulating that memory that was allocated to provide storage for the .bss section.

like image 68
Nick Meyer Avatar answered Oct 05 '22 20:10

Nick Meyer