What does it mean for memory to be allocated at "compile time"?

166 Views Asked by At

My understanding is that static memory such as the stack, virtual tables, static variables, ect are all allocated at "compile time" as opposed to dynamic memory which is allocated at runtime. But I'm confused as to what this means. As I understand it the compiler/linker interprets the written code, translates it into machine language, and then assembles it into an executable. Is the implication that the stack and all static memory are part of the executable file itself?

1

There are 1 best solutions below

3
eerorika On

My understanding is that static memory such as the stack ... are all allocated at "compile time"

Sort of. It is determined at compile time how the memory will be allocated when a block is entered. But the allocation doesn't happen until runtime.

Is the implication that the stack and all static memory are part of the executable file itself?

Depends on what you mean by that. The executable file does indeed contain information about memory that will be allocated. For example, if the compiler knows that it needs to allocate 64 bytes on the stack, then there would be an instruction that adjusts the frame pointer by 64 bytes (or something along those lines).

None of this is specified by the language itself, so everything is up to the language implementation to decide and varies between different language implementations. My explanation is simplified and describes a hypothetical language implementation and might not be accurate to whatever language implementation you use.