Run-Time Memory Flashcards
(10 cards)
What is a virtual address space in a multi-tasking operating system?
Each process runs in its own isolated memory space
Each virtual address space starts at memory address zero
The operating system maps virtual memory addresses to physical memory addresses using page tables
The virtual address space is usually contiguous
What are sp and bp/fp in the context of memory management?
sp (stack pointer): Points to the top of the current stack
bp/fp (base/frame pointer): Points to the base of the current stack frame
Used to manage function call frames and local variables
Describe the steps that occur during a function call in terms of stack management?
Push the current frame pointer onto the stack
Move the current stack pointer to the frame pointer
Subtract space needed for the function’s local data from the stack pointer
Execute the function code (local variables accessed via negative offsets from frame pointer)
On function exit, restore the previous stack and frame pointers
What are the key differences between the heap and the stack?
Stack:
Used for automatic (local) variables
Memory allocation/deallocation is automatic
Fixed size, managed by function calls
Heap:
Used for dynamic memory allocation
Manual memory management (malloc/free)
Can cause memory fragmentation
Larger and more flexible memory space
What happens during deep recursion?
Each recursive call creates a new stack frame
Eventually, the stack fills up
Leads to a “too many recursions” error or stack overflow
Limited by available stack memory
How does a CPU translate virtual memory addresses?
Uses Memory Management Unit (MMU)
Employs Translation Lookaside Buffer (TLB)
- A cache of recently-used memory address mappings
Operating system maintains page tables for address translation
What typical information is stored in a stack frame?
Function parameters
Local variables
Previous frame pointer
Return address
Saved register values
What are some common heap memory allocation strategies?
Best Fit: Find the smallest free block that fits the request
First Fit: Allocate the first free block large enough to fit the request
Next Fit: Similar to First Fit, but starts searching from the last allocation point
How does a recursive function appear in the stack?
Each recursive call creates a new stack frame
Frames are stacked on top of each other
Each frame contains its own local variables and parameters
Frames are removed as recursion unwinds
On a 64-bit machine, what are the key memory management registers?
%rsp (Register Stack Pointer): Points to the top of the stack
%rbp (Register Base/Frame Pointer): Points to the base of the current stack frame