1/55
FIX LATER
What is the heap used for?
Dynamic memory that can be allocated and deallocated at runtime
What are the two parts of heap memory?
Heap data and heap management
What is heap data?
Memory that can be allocated and deallocated
What is heap management?
A table that tracks which heap memory is allocated and which is free
Where is the heap management table stored?
In the heap area, not in registers
What are the steps to allocate n units from the heap?
Find a free block of size n in the heap management table, mark it used, and return its address
What are the key features of heap memory in ANNA?
Only allocation is supported, with no deallocation
How does ANNA implement heap management?
With a single pointer to the next free word in the heap
In ANNA heap memory, what does memory before the heap pointer represent?
Heap memory that is already used or allocated
Where is the heap placed in ANNA memory layout?
After code and data, before the stack
What is the ANNA memory layout order?
Code, Data, Heap, Stack
Where does the ANNA stack start?
At address 0x8000
For int *p = new int[n] in ANNA, what does p receive and what happens to the heap pointer?
p receives the current heap pointer, and the heap pointer advances by n words
For Node *p = new Node with int val and Node *next, what memory is allocated and how are fields set?
Two words are allocated, val is stored at the first word, and next is stored at the second word
What is a function call in assembly?
A transfer of control to a function label with arguments and return address arranged by the calling convention
What is the return address of a function call?
The address of the instruction after the call, used by ret to resume the caller
What are typical caller responsibilities in a calling convention?
Place arguments, save caller-saved registers if needed, call the function, and retrieve the return value
What are typical callee responsibilities in a calling convention?
Save callee-saved registers, set up a frame, execute the body, restore state, and return the result
What is a function prologue?
Code at function entry that sets up the stack frame and saves needed registers
What is a function epilogue?
Code at function exit that restores the stack and registers and returns to the caller
How do you write a simple complete assembly function?
Use a label, prologue, body, epilogue, and ret while following the calling convention
How do you write a function call in assembly?
Set up arguments, call the function label, then use the return value and clean up as the convention requires
What hardware support is used for function calls and returns?
call pushes the return address and jumps to the function, and ret pops the return address and jumps back
What is stack memory used for?
Function call management, local variables, parameters, return addresses, and saved registers
What is an activation record?
The stack frame for one function invocation containing parameters, locals, saved registers, and return address
What is the stack pointer?
A register that points to the current top of the stack
What is the frame pointer?
A register that points to the base of the current activation record
Why use a frame pointer?
It gives stable access to locals and parameters even when the stack pointer changes
What is a calling convention?
An agreement on argument passing, return values, register preservation, and stack cleanup
In which direction does the stack typically grow?
Downward toward lower addresses
What does push do?
It decrements the stack pointer and stores a value at the new top of stack
What does pop do?
It loads a value from the top of stack and increments the stack pointer
What happens to activation records during recursion?
Each recursive call gets its own activation record on the stack
What is a segmentation fault?
Accessing an address outside the program's allowed segment
What is a null pointer dereference?
Dereferencing a pointer whose value is NULL
What is stack overflow?
The stack limit is reached, so there is no space for another activation record
What is heap overflow?
The heap limit is reached, so an allocation cannot be satisfied
What is array out-of-bounds?
Using an index outside the bounds of an array
What is buffer overflow?
Writing data beyond a buffer boundary and overwriting adjacent memory
Why do array out-of-bounds and buffer overflow often not cause segmentation faults?
The adjacent overwritten memory is still within the program segment
What is double free?
Deallocating memory that was already deallocated
What is invalid free?
Deallocating memory that was never dynamically allocated or not using the block start address
What is dangling pointer dereference?
Dereferencing a pointer to memory that was previously deallocated
What is a memory leak?
Failing to deallocate memory once it is no longer needed
What are ways to handle memory errors?
Exceptions, garbage collection, language limitations, programmer detection, and crashes or aborts
How do exceptions handle memory errors?
They automatically detect and throw an exception, the program can catch and recover, and an uncaught exception aborts the program
How does garbage collection handle memory errors?
It automatically deletes unneeded memory, requires no explicit deallocation, and makes leaks less likely but still possible
What is the language limitation approach to memory errors?
Restrict pointer operations so pointers cannot point to invalid memory
In C++ vs Java, which memory errors are not possible in Java?
Segmentation fault, double free, invalid free, and dangling pointer dereference
In C++ vs Java, how does Java handle null dereference, stack overflow, heap overflow, array out-of-bounds, and buffer overflow?
It handles them as exceptions
In C++, how are array out-of-bounds and buffer overflow detected?
They are usually not detected unless they cause a segmentation fault
Why does C++ not detect array out-of-bounds?
It does not perform automatic bounds checks, often for performance
What are problems with C++ not detecting array out-of-bounds?
Silent memory corruption, security bugs, and incorrect output
In C++, what happens for segmentation fault, null dereference, stack overflow, double free, and invalid free?
The program crashes
In C++, what happens for heap overflow?
It raises an exception
In C++ and Java, when is a memory leak detected?
It is not detected until heap overflow