Is stack memory allocated at runtime or compile time?

Is the stack allocated at runtime or compile time?
Example:

void main() { int x; scanf("%d", &x); int arr[x]; } 
+7
source share
8 answers

The stack is allocated at runtime; however, the layout of each stack frame is determined at compile time, with the exception of variable size arrays.

+5
source

how would you allocate compile time? if I compile the code on my machine, but execute it on yours, how can the compiler preallocate the memory for the stack on your computer?

+2
source

This should help. Stack storage is allocated at runtime.

Keep in mind that it must be allocated at runtime because the compiler does not know how many times you call the function or how many times the while loop or something else executes.

+2
source

It must be allocated at runtime. Consider the following:

 void a( void ) { int x; } void b( void ) { int y; a(); } int main( void ) { a(); b(); } 

The address of the local stack element in a () will be different between its two calls. As blinkenlights note, the layout of each frame of the function stack is largely determined at compile time, but the location of this frame is determined at run time.

+2
source

The stack is always allocated at runtime, you need a stack to execute the method, not to compile.

On similar lines

+1
source

To complement all the other answers (which are correct in the general case), it is sometimes theoretically possible to allocate a stack at compile time (depending on your definition of "allocate").

In particular, if your program does not have function pointers or recursions, then you can use static analysis to determine the maximum stack size. Indeed, some built-in compilers do just that.

+1
source

The Ofcourse column is highlighted at runtime. You need a stack stack to execute .ec code

Mark the link that discusses the memory layout of program C.

0
source

Check out this great article.

http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory

This is a great entry explaining the program memory. You can also check other articles by the same author regarding the behavior of memory in the s system, which will give you an idea of ​​the actual work in memory.

if you want to know all about memory, try reading this article by Ulrich Draper http://www.akkadia.org/drepper/cpumemory.pdf

hope this helps!

0
source

All Articles