2

int x=10;

int y=0;

int z;

static int m = 0;

these are my 3 global variables.

i know that __ uninitialized global data__ goes to the .BSS segment, but what about the global data initialized to 0.

in which segment would the variable y and m would be stored.

is there any compiler related dependencies here?

hanish
  • 67
  • 6

2 Answers2

3

The variables y and m which are statically allocated global variables initialized with a value consisting solely of zero-valued bits may be moved to the bss section. This has compiler dependency.

Compiler is free to put such variable into bss as well as into data.

GCC has following compiler option to decide on this:

-fno-zero-initialized-in-bss

If the target supports a BSS section, GCC by default puts variables that are initialized to zero into BSS. This can save space in the resulting code. Above option turns off this behavior.

MSVC has following preprocessor directive available:

#pragma bss_seg

Aravind
  • 555
  • 1
  • 3
  • 12
0

(not my own words)

In C, statically-allocated objects without an explicit initializer are initialized to zero (for arithmetic types) or a null pointer (for pointer types). Implementations of C typically represent zero values and null pointer values using a bit pattern consisting solely of zero-valued bits (though this is not required by the C standard). Hence, the bss section typically includes all uninitialized variables declared at file scope (i.e., outside of any function) as well as uninitialized local variables declared with the static keyword. An implementation may also assign statically-allocated variables initialized with a value consisting solely of zero-valued bits to the bss section.

http://en.wikipedia.org/wiki/.bss

z is uninitialized, so it will bet put in BSS. But didn't you say that in your question?

y and m would also likely go there. Not sure if you were intending to ask about them, though.

X would then go to the data segment.

http://en.wikipedia.org/wiki/Data_segment

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • i wanted to know about y and m . about x and z is was sure about .bss but had a doubt regarding y and m. – hanish May 31 '13 at 04:29
  • from what I see, it's compiler dependent, but likely it goes to BSS - relevant section is bolded above. – xaxxon May 31 '13 at 04:47