Question:
What does the heap and stack physically look like in RAM?
Answer:
- Both the stack and the heap are physically in RAM (we do not consider architectural dislocations using special processors / computers)
- Their size and location are determined by the axis
- In this case, the heap can be fragmented (sometimes quite strongly). Axes usually have special routines for defragmenting the heap.
- The stack is usually never fragmented (you can probably think of fragmented stack implementations, but that's an oxymoron).
- The stack is, as it were, faster because the only parameter it works with is the stack pointer (usually a register) – therefore, all operations with the stack work many times faster than with the heap. The
POP/PUSH
write operation from the stack is 1 body movement of thePOP/PUSH
processor - It is more difficult with a heap precisely because of its fragmentation, and a simple operation of extracting a value from it can result in dozens (if not hundreds) of processor movements.
- The disadvantages of the stack are that it is small in size (it is always an order of magnitude smaller than the heap) – and also that access to it is only sequential.