Memory Management Flashcards
What is layout of x86 / x86_74 memory structure (top to bottom)
- Code/text segment
- Initialised data segment
- Uninitialized data segment
- Heap / Stack
What is the in the text/code segment?
Binary instruction set of the program (what the program does). Read only.
What is in the initialised data segment?
- Where you store Global + static variables with non-zero values. Values are copied from the text/code segment
- Initiated before program execution
What is in the Uninitialized data segment (.bss)?
Stores Global and static variables initialised to zero or not explicitly initialised in our code. Initiated before program execution.
What does the stack store?
Variables created and removed through execution. First in last out.
What does a stack pointer provide?
It points to the next available memory address.
What happens to the stack pointer when a variable is created?
When a variable is created, the stack pointer increases by the size of variable in bytes.
What happens to the stack when a variable is removed?
The stack pointer is decremented by the size of variables in bytes. The values aren’t cleared.
What happens to the stack when you enter a function?
When you enter a function it pushes a stack frame onto the stack.
What does a stack frame for functions contain?
A stack frame contains the return address where the function was called from, and any argument variables.
What happens to the stack when a function declares a variable?
The variable is pushed onto the stack during the execution of the function call (length of the scope).
What order are local variables removed from the stack on return of a function?
On returning from a function, local variables are removed in reverse order of their creation. (Push onto the top, pop off the top)
How big is the stack?
Size of the stack is fixed and known at execution time
When do stack overflows occur?
- If we make a large number of variables or large variables within a function
- Recursively call a function too many times.
What is the Heap?
Memory dynamically allocated by the programmer.
What are some guidelines for using the Heap?
Memory must be allocated and deallocated.
What does the allocator function do?
Manage heap, reserving space and not double allocating it.
What is the size of the heap?
Heap can use as much memory your system has available. The heap dynamically grows and shrinks, it starts at the end of the uninitialized data segment.
What does the allocator function return?
Returns an address in memory reserve based on our request.
When can allocation fail (Heap)?
When we run out of RAM
What can you do with an address that has been deallocated?
Nothing, it cannot be used again. Read/write operations to that address are terminated.
When can a SEGFAULT error be cause in memory managment?
When writing to an incorrect memory address.
How do you manage memory in C (allocating/deallocating heap)?
functions:
- malloc()
- free()
How do you manage memory in C++(allocating/deallocating heap)?
Keywords:
- new/new[]
- delete/delete[]
What is the parameter for malloc()
(unsigned int size), the size bytes of memory on the heap.
What doe malloc() return?
Returns the address to start of reserved memory.
What does free(void *ptr) do?
Takes a pointer that points to something and removes the reservation of memory it was allocated with (frees memory).
How do the stack and the heap differ?
Stack memory is a sort of memory allocation that the OS continuously manages and uses to store local variables in a FILO order. On the other hand, heap memory is a type of dynamic memory allocation used for storing objects and data structures that require a longer lifespan than stack memory.