Heap Management, Control Flow, Coroutines, and Object-Oriented Programming Flashcards
(50 cards)
What is heap fragmentation?
Occurs when allocated memory blocks are freed, leaving gaps that may not fit new allocations.
What are common allocation strategies for heap memory?
- First fit
- Best fit
- Next fit
How can you inspect the stack frame layout?
By using printf to display the addresses of parameters and local variables.
What is the role of the frame pointer?
It is the base of the current function call.
What is the role of the stack pointer?
It indicates the top of the stack.
What tools can be used to inspect compiled binaries?
- objdump (CLI disassembler)
- Boomerang (GUI decompiler)
Fill in the blank: The stack is used for _______ and function frames.
Local vars
Fill in the blank: The heap is used for _______ memory.
Dynamic
True or False: Each process has its own space managed by the OS using page tables.
True
What does a jump do in programming?
Moves the program counter (PC) to a new instruction address.
List three uses of jumps in programming.
- Control flow (loops, conditions)
- Error handling
- Special tricks like coroutines
What is an unconditional jump in assembly?
jmp
What are conditional jumps used in assembly?
- jg
- je
- etc.
What is the purpose of the goto statement in C?
Transfers control to a labeled statement.
What is one pro and one con of using goto in C?
- Pro: Useful for error handling or cleanup
- Con: Can lead to spaghetti code
True or False: Dijkstra’s paper states that goto is beneficial for programming.
False
What does the goto statement allow for in cleanup operations?
Acts like try/catch for low-level cleanup.
What is the example of using goto for cleanup in C?
if (!(p = malloc())) goto errexit;
What is setjmp() used for?
Saves CPU state (registers, stack, etc.) into a jmp_buf.
What does longjmp() do?
Restores saved state and jumps back to the setjmp call.
Fill in the blank: setjmp creates a _______.
Savepoint
How does error handling with setjmp/longjmp work?
setjmp captures state, longjmp restores it on error.
What is the main difference between subroutines and coroutines?
Subroutines have one-way calls; coroutines yield back and forth.
What happens to the stack when using coroutines?
Stack stays intact.