views:

150

answers:

4

This question is a follow up of my previous question

http://stackoverflow.com/questions/3572610/stack-growth-direction

I would like to know whether stack is created by a compiler or OS/architecture ? Also how does OS knows about these compiler specific things ? For ex: C++ allows variables to create data on stack and heap while java allows only heap.

Also if stack is created on heap as mentioned in the post how can system know about it because the system knows only about stack pointer and base pointer.

+3  A: 

The stack is most definitely defined by the compiler, the OS allocates the space for it but that is relatively trivial. The stack is a dedicated place in memory that is used by the compiler (in so much as the compiler defines the instructions that use it) to control program execution flow and store local variables etc.

So the OS doesn't know about the compiler specific stuff. The stack is still stored in main memory it is just not part of memory that you (the programmer) can directly control.

radman
+4  A: 

The stack is a memory location allocated for your program by the OS. Once it's allocated, the OS sets a register (on x86, it's esp) to where the stack is and then it starts your program. Compilers know that if they use the value in this register as the stack pointer, they'll be okay. Then they do whatever they want to do with it. The OS just allocates a zone. It doesn't care about how it's used after.

The OS doesn't know if your program will use mostly the stack or the heap. However, since most programming languages use the stack in a way or another, it knows it should allocate one. For instance, Java stores its objects on the heap, but most implementations of the JVM will use the stack to maintain call frames (and primitive local variables), so it needs the stack too.

zneak
@zneak - for Java, it should read **a stack**, as we have one jvm stack for each running thread.
Andreas_D
In the executables header there is often a field for the mainthreads stacksize (field is set by the compiler and can normally be set with a linker option). Also, when creating stacks, one can often set the threads stacksize.
Markus Kull
JVM thread stacks may or may not be the same memory as the actual java program. This is complicated to talk about, but the JVM could keep up with a separate stack for each java thread from the its system stacks that it is using for it's native code.
nategoose
A: 

Traditionally the stack is the location where the return address for machine code calls were placed (so it can return when done). Hence there were instructions to easily access this location in memory.

It was quickly found that putting arguments to the call along with the return address was a very simple and efficient way of doing it. This has then grown to deal with local addresses etc.

Thorbjørn Ravn Andersen
+1  A: 

One Java virtual machine stack is created for each thread in the virtual machine. The stack stores frames and the content can not be manipulated directly except for push and pop operations of frames.

Frames are created each time a message is invoked and is used to store data and partial results, as well as to perform dynamic linking , return values for methods, and dispatch exceptions.

The language spec details, that a Java virtual machine stack is analogous to the stack of a conventional language such as C. So to me, it is obvious that the jvm stack model is coded and used in the jvm implementation and not provided by the host OS.

Andreas_D