Reputation: 3233
I wanted to know that where will the uninitialized data be stored after it has been modified?
For instance,
all the variables which are initialized to some value in the code will be stored in the .data section.
all the variables which are not initialized, will be initialized to 0 by the compiler and stored in the .bss section.
Now, let's say, I define an array of 10 integers in the code. But I do not specify the elements of this array. So, all the integers of the array will be initialized to 0 by the compiler and 40 bytes will be reserved in the .bss section.
After this, I write some data to the array, will it modify the data in .bss section itself?
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
int i;
/* 10 integers are initialized to 0 and stored in the .bss section */
unsigned int numbers[10];
/* write data to array */
for(i=0;i<10;i++)
{
numbers[i]=pow(2,i);
}
Upvotes: 0
Views: 1921
Reputation: 958
Already was said in previous responses that in your example the numbers array will be stored on stack and not on .bss section and that only global and static variables will be initialized to 0 if they are numeric types or NULL for objects
The following example shows where different variables will be stored
int abc = 1; ----> Initialized Read-Write Data
char *str; ----> BSS
const int i = 10; -----> Initialized Read-Only Data
main()
{
int ii,a=1,b=2,c; -----> Local Variables on Stack
char *ptr;
ptr = malloc(4); ------> Allocated Memory in Heap
c= a+b; ------> Text
}
Also there is nice description here
Upvotes: 1
Reputation: 22104
You are confusing two things here. If you talk abouit .bss
or .data
section your are talking about local (as in your example) versus global/static variables.
Local variables, as in your example, are allocated on the stack. There is no .bss/.data
involved. This data will be uninitalized and has a randome value, whatever happens to be the value on the stack.
For global/static uninitialized variables, they are initiliazed to 0 when the program starts.
Upvotes: 2
Reputation: 122443
all the variables which are not initialized, will be initialized to 0 by the compiler and stored in the .bss section.
No, only global variables and static variables that are un-initialized will be initialized to 0.
Upvotes: 0
Reputation: 3307
In your example the array is declared on the stack
.
Thus nothing goes on to .bss
Upvotes: 0
Reputation: 36476
The .bss contains global and static uninitialized variables. In your example, the array will be placed on the stack.
Upvotes: 4
Reputation: 11232
Your assumption that
"all the variables which are not initialized, will be initialized to 0 by the compiler "
is not true. It is uninitialized, the memory for the variable is allocated but the content is uninitialized.
Upvotes: 1