📚

Stack

CategorySystem

Overview

When a program is started by the operating system, a fixed block of memory is allocated in system memory for the program stack.

The program stack is also known as the call stack.

💡
The size of the call stack is fixed and the size is platform-dependent.

Call Stack

The portion of the stack allocated for a single function call is called a stack frame.

The stack behaves like a Last In, First Out (LIFO) queue.

When a function is called, a new frame is pushed onto the stack.

When the function returns, the frame is popped from the stack.

The calling convention of a function determine what is pushed on the stack, and in what order.

The stack contains:

The current position on the stack is determined by the stack pointer.

The direction in which the stack grows depends on the processor architecture.

In the Intel x86 architecture, the stack grows down.

Therefore, when memory is allocated on the stack (push), the stack pointer decreases; and when memory is freed from the stack (pop), the stack pointer increases.

Registers

The stack frame is managed by two CPU registers: the stack pointer and the base pointer.

The base pointer points to the bottom of the stack frame (BSP on Intel x86).

The stack pointer points to the top of the stack frame (ESP on Intel x86).

When a function is called, we need to save the current position on the stack, to allow the program to resume execution after the function returns.

When a function is about to be called:

pushl %ebp
movl %esp %ebp

When the current function returns:

movl %ebp, %esp
popl %ebp
ret

Allocation

It is possible for a program to allocate memory on the stack by changing the value of the stack register.

This is useful for allocating temporary memory efficiently, but the amount of free memory is very limited.

Languages

C and C++

Any type can be allocated on the stack as long as there is enough space.

Additional memory can be allocated on the stack using the _alloca function.

C#

Only value types can be allocated on the stack.

A ref struct can only be allocated on the stack.

A reference type can be allocated on the stack when it is allocated with stackalloc.