Stack
Source material:
Properties of the stack:
a designated block of RAM
when a program starts, the kernel allocates a block of memory for each thread in program's stack
- this can be inspected at
/proc/$PID/maps
- this can be inspected at
data can only be added (
push) or removed (pop) from the top (Last In First Out)it has a fixed, pre-allocated size that cannot be exceeded. uncontrolled recursion can lead to "stack overflow" and crash a program.
The stack is considered a very fast memory allocation mechanism because it only requires adjusting a single stack pointer to allocate or deallocate memory.
Properties of the stack pointer:
- it exists in one of the CPU registers, which is the fastest type of memory available to a computer
- it only holds one thing: the memory location of the "top" of the stack
- CPU instructions like
CALL(call a function) andRET(return from a function) can modify it almost instantaneously