We say that global variables and static variables are always initialized to 0. Then my question is, why do we have separate sections in a binary for initialized and uninitialized data.
I wrote the following code –
int i;
int j=0;
static int k;
static int l=0;
int main()
{
static int m=0;
static int n;
printf("%d, %d\n",i,j);
printf("%d, %d\n",k,l);
printf("%d, %d\n",m,n);
return 0;
}
And the output was –
0, 0
0, 0
0, 0
I checked output of objdump of bss section and only this section contained the variables. But as per the link –
http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html
Typically, in each process, the virtual memory available to that
process is called its address space. Each process’s address space is
typically organized in 6 sections that are illustrated in the next
picture: environment section – used to store environment variables and
command line arguments; the stack, used to store memory for function
arguments, return values, and automatic variables; the heap (free
store) used for dynamic allocation, two data sections (for initialized
and uninitialized static and global variables) and a text section
where the actual code is kept.
So, I am just confused. If we have two data sections why is all the data placed in .bss section. And also I wanna understand what does .data contain.
Can someone please help me on this?
The .data section is typically reserved for variables with values known at compile time or larger blocks of constant memory such as strings that are known at compile time and static array blocks. Also the .bss section stores unintialized or zero valued variables because storing zero’es in the .data section wouldn’t make much sense.