1

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

I am wondering where global variables and static variables are stored in C/C++. as far as local variables are stored in stack when function is active we also request memory from heap but I have no clue about static and global variables. Could any one please throw light on it.

Community
  • 1
  • 1
samprat
  • 2,150
  • 8
  • 39
  • 73

3 Answers3

3

They live in global memory, which is separate from the other two areas. If their precise contents are known at compile time, they will be emitted as part of the program or library and live in the data segment.

Note: I've always thought that it lived with the code in the text segment, but a brief hunt around clearly indicates that the data segment is separate from the text segment, though they are generally contiguous.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
2

When loading an executable, the operating system allocates memory for the global data of the program. Usually they are kept in the .data and .bss sections of the executable.

vhallac
  • 13,301
  • 3
  • 25
  • 36
0

They are stored in the data segment, which is typically (always?) of a fixed size, and "burned in" to the executable, unlike the heap which is allocated from the operating system at run-time. The data segment is usually divided into initialized and uninitialized data sections (.data and .bss respectively).

Wikipedia has more information here

clstrfsck
  • 14,715
  • 4
  • 44
  • 59