1

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);
    }
Neon Flash
  • 3,113
  • 12
  • 58
  • 96
  • It will modify the data in the array, wherever it's currently stored. – Kneel-Before-ZOD Jul 25 '13 at 06:50
  • Possible duplicate of [Where are static variables stored (in C/C++)?](http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c) ? – Nobilis Jul 25 '13 at 06:54
  • This information may also help [unassigned value in the int](http://stackoverflow.com/questions/17828567/unassigned-value-in-the-int/17828594) in regard to the question what values are assigned to unassigned arrays. – bkausbk Jul 25 '13 at 06:59

6 Answers6

4

The .bss contains global and static uninitialized variables. In your example, the array will be placed on the stack.

tskuzzy
  • 35,812
  • 14
  • 73
  • 140
2

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.

Devolus
  • 21,661
  • 13
  • 66
  • 113
1

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.

Asha
  • 11,002
  • 6
  • 44
  • 66
1

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

LZR
  • 948
  • 10
  • 28
0

In your example the array is declared on the stack.

Thus nothing goes on to .bss

Jay D
  • 3,263
  • 4
  • 32
  • 48
0

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294