4

C++ allows the programer to use either stack allocated memory or dynamic memory on the heap. I am fairly clear on how the stack operates (I have created stacks as exercises in C++) but am still curious about how and where "the stack" is defined.

Is "The Stack" a feature of the language, a feature of the OS or a feature of the hardware? I have a vague idea that each stack is something relating to each of the many active thread processes from the perspective of the operating system. This suggest to me that it's implemented on the software level and not a hardwired feature of the CPU or motherboard.

I imagine, in the case of a C++, the compiler might have inserted an implementation of a stack into each binary. Or maybe the OS applies it's own stack implementation into each running process? How and were is this defined?

I found a similar question here but I don't see it answered and my hope is to re-state the question more directly.

Community
  • 1
  • 1
Jeff-Russ
  • 341
  • 2
  • 10
  • "I have created stacks as exercises in C++" – different kind of stack `:)` don't let the name fool you. http://en.wikipedia.org/wiki/Stack-oriented_programming_language. And, the question you linked _is_ answered. – Matt Ball Mar 28 '15 at 19:45
  • 1
    http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap exact duplicate with a wider view – GorvGoyl Mar 28 '15 at 19:47
  • Yeah I'm aware. I was just saying i have made "stacks" to set it straight that I didn't need explanations about what "push" "pop" mean etc... – Jeff-Russ Mar 28 '15 at 19:48

2 Answers2

2

"Is "The Stack" a feature of the language, a feature of the OS or a feature of the hardware?"

It's a feature of the hardware actually. Most CPU's provide the feature of a stack pointer register, that is used for call instructions. Which part of memory should be reserved for it is actually OS (bare metal) implementation dependent.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

The stack is a feature of hardware managed by the operating system. Certain instructions (e.g., procedure call) affect the stack. There will be hardware support for context switches and processor mode switches.

The operating system manages the stack itself. The OS allocates the stack(s). There is usually a separate stack per process and per processor mode.

Stacks usually grow downwards. The hardware instructions that add a value to the stuck usually decrement the stack point register. Virtual memory systems normally place the user stack at the highest part of the user address space.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • So does this mean that the hardware imposes limitations on the stack(s) the maximum number or stacks at a time or the size of each stack? – Jeff-Russ Mar 28 '15 at 19:52
  • The OS will expect certain stacks to exist. Usually that is one stack per thread in user mode and a stack per process (or possibly thread) in protected modes. In theory an OS could implement multiple stacks per thread and switch among them (that's how older threading worked). Generally this does not happen. The hardware limitation is on the number of stacks that can be used at one time; not the number of stacks that can be created. – user3344003 Mar 28 '15 at 19:55
  • More space: The hardware sees a stack as a block of memory with a stack pointer register that points to it. The OS can change the register to point to the memory to choose. – user3344003 Mar 28 '15 at 19:58