1

Lets say I have the following program. Which part of memory is a allocated in? Also is the behavior same for both c and c++?

// a is allocated in the ?
int a[3] = {1, 2, 3};

int main()
{
    // x is allocated in the stack
    int x[3] = {4, 5, 6}

    // y is allocated in the heap
    int* y = malloc(sizeof(int)*3);
}
Farhad
  • 4,119
  • 8
  • 43
  • 66
  • 1
    C does not specify this. It is an implementation detail. – chux - Reinstate Monica Oct 09 '17 at 22:36
  • 1
    Neither does it specify a stack or a heap. –  Oct 09 '17 at 22:38
  • 'Global' and 'statically compiled' don't mean the same thing, and the latter is meaningless. – user207421 Oct 09 '17 at 23:11
  • @EJP I'm sorry for my lack of knowledge. But I can't seem to think of a single case where a global variable is not statically compiled. Unless of course you mean the reverse scenario (which I assume you are) referring to how not all statically compiled variables are global. Am I correct? –  Oct 09 '17 at 23:40
  • https://electronics.stackexchange.com/questions/237740/what-resides-in-the-different-memory-types-of-a-microcontroller/237759#237759 – Lundin Oct 10 '17 at 06:59
  • 1
    @AlanCPSC There is no term called "statically compiled". A variable declared at file scope ("global") will have _static storage duration_, meaning it will be initialized before main() is called and that it will persist throughout the execution of the program. – Lundin Oct 10 '17 at 07:01

3 Answers3

2

Where they are allocated is dependent on your machine architecture and your compiler and linker implementation (neither of which you have specified). the C++ Language Standard has nothing to say on the subject.

Farhad
  • 4,119
  • 8
  • 43
  • 66
  • Ok, they are not "defined in the specification" - but in practice where are they "usually"? –  Oct 09 '17 at 22:40
  • @AlanCPSC An optimizing compiler could simple `return 0;` and not have any storage for `a[]`. – chux - Reinstate Monica Oct 09 '17 at 22:41
  • 1
    Please re-read my answer - there is no "usually", it depends on your platform and compiler/linker. –  Oct 09 '17 at 22:41
  • I'd be very interested if the downvoter would point out what piece of my answer is inaccurate. –  Oct 09 '17 at 22:50
  • 1
    In the real world outside the standards, where computers exist, there _is_ a usually. The vast majority of all linkers use segments called `.data` and `.bss`. This is true from tiny micocontrollers to 64 bit PCs and everything in between. I'd be curious to hear what systems there are that don't use these names. – Lundin Oct 10 '17 at 07:05
1

In static storage, to use standard speak. It doesn't really say much of anything about how static storage should be implemented, other than that it should endure for the whole time of the program and that it should be implicitly zero initialized if no nonzero initializer is given.


Practically in ELF binaries, these variables are all concatenated into sections, which get, at load time, mapped onto segments, which are basically memory blocks with certain memory protection bits on or off. If the global variable is writable and initialized with a nonzero value, it'll go into an ELF section designated as .data. zero-initialized variables will go into .bss (not part of the binary image so as to save space) and const static variables will go into .rodata, which will get mapped read-only, to facilitate write protection.

Your compiler's binutils (such as nm or objdump) can allow you to peek into the (implementation-dependent) details.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
0

This is an implementation detail, the same is true for stack and heap. The language C doesn't have such concepts. If your implementation uses a heap, it probably also uses segments in a binary format provided by the OS. In that case, static variables are placed in a data or bss segment, so they are either part of the program itself (data) or allocated by the OS when loading the program (bss).

A somewhat common approach is to place default-initialized variables in bss because this way, they don't increase the size of the executable file. For constant data, there's often a rodata segment available, many C compilers put string literals there.

But bottom line is: you shouldn't care as C doesn't specify this and there are platforms that don't provide segments, or a heap, ...