Memory Management Flashcards
What are the four areas of memory?
Code segment, data segment, function call stack, heap
What does the code segment contain?
Program instructions, addresses of functions
What does the data segment contain?
Global variables, static variables, literals
What is the function call stack used for?
Managing the order of function calls, storing local variables (including parameters)
What is the heap?
Part of the data segment, it stores all dynamically allocated memory
What is a stack?
A data structure analogous to a stack of dishes. Data is pushed in and popped out in a last-in, first-out fashion.
What is the function call stack?
It manages the function-call-and-return mechanism
What does the function call stack contain?
Automatic variables, return address of the next instruction in the calling function
What is control flow?
The order in which instructions are executed
How does control flow work?
Begin at first instruction in main() function, continue sequentially to next instruction, when a function is called, control transfers to the first instruction of that function, and when returned, control is transferred back to the calling function
How does the function-call-and-return mechanism work?
When a function is called, a new stack frame is created and pushed onto the function call stack. The top frame always belongs to the function currently executing. When a called function returns, its stack frame is popped off, memory from local variables freed up, and control transferred back to the calling function.
What does a stack frame contain?
Automatic variables (parameters and locals), and the address of the next instruction in the calling function, where control will be transferred after the called function ends.
What is the heap?
A portion of program memory used for dynamically allocated memory
What is static allocation?
Memory allocation that happens at compile-time. Memory is allocated statically simply by declaring variables.
What is dynamic allocation?
Memory allocation that happens at run-time. In C we do it by using the malloc() library function