2

Possible Duplicate:
Where are static variables stored (in C/C++)?

I've read that all global variables that are initialized will be allocated space on the initialized data segment and all static and global variables that are not initialized are initialized to 0, and allocated on the BSS. In case of the following definition,

static int i = 0;

where will space for i be allocated? Will it be on the initialized data segment because i is initialized, or will it be on the BSS since the value of i is 0?

Community
  • 1
  • 1
Raj
  • 4,342
  • 9
  • 40
  • 45
  • 1
    have a look at this: [question-1294772](http://stackoverflow.com/questions/1294772/does-gcc-automatically-initialize-static-variables-to-zero) – sleepsort Dec 11 '12 at 12:27
  • Is this a question about what actually happens on some specific platform, what is required to happen by the standards, what is likely to happen, or what? – David Schwartz Dec 11 '12 at 12:31
  • I wanted to know what kind of behaviour it will exhibit, and if the behavior is well-defined, what is required to happen. – Raj Dec 11 '12 at 12:32
  • @Raj The behavior of the code is well defined. Whether that ends up in a data segment, a BSS segment, or something else entirely, is 100% dependent on a particular combination of platform and tools. – nos Dec 11 '12 at 12:38
  • @Raj - Keep in mind with my answer (below) that it's implementation dependent... but this is true for Ubuntu/GCC. You can use a similar method to look at other systems. – Mike Dec 11 '12 at 12:46

3 Answers3

9

Yes, non-initialized static variables will be initialized to 0 by default, that's always true in C.

The storage location of the data will be implementation dependent... I've seen that it's the 0 initialized static variables (i in your case) that goes in .BSS (Block Started by Symbol).

Non-0 initialized statics go into .DATA static int i=2; for example.

To show the point:

int main(int argc, char * argv[])
{
    return 0;
}

saved in "test.c"

> gcc test.c
> size a.out
text     data     bss   dec    hex   filename
1056     252      8     1316   524   a.out

Then we update it as such:

int main(int argc, char * argv[])
{
    static int i;
    return 0;
}

> gcc test.c
> size a.out
text     data     bss   dec    hex   filename
1056     252      12    1316   524   a.out

Change it again as such:

int main(int argc, char * argv[])
{
    static int i = 2;
    return 0;
}

> gcc test.c
> size a.out
text     data     bss   dec    hex   filename
1056     256      8     1316   524   a.out
Mike
  • 47,263
  • 29
  • 113
  • 177
0

This really depends on the actual compiler/implementation, but yes, i would most likely be on the BSS because it's either on file level (i.e. outside any function) or static and inside a function and has a value of 0.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • I believe that neither file scope and function scope are relevant to storage. There is no reason for scope to affect the location of a non-automatic variable; scope is merely managed while the code is being compiled to disallow generation of memory access out of scope. I agree though that it is compiler dependent. – mah Dec 11 '12 at 12:32
0

It's implementation dependant, on Linux with gcc 4.5.2 when I compile this program:

static int a[1000000] = {1}; void main() {}

I get executable with size 3.9M - first element of the array is initialized (with non-zero value) so array 'a' goes to .data segment.

When I initialize array with zeros:

static int a[1000000] = {0}; void main() {}

I get executable with size 8.2K - I guess that such difference in size indicates that this time 'a' array was located in .bss segment.

mzet
  • 577
  • 2
  • 7