Memory in C
Divided into 5 segments
Stack
Used to store automatics and activation records for functions
-located at the top of writable memory (grows downwards)
Heap
Stores explicitly requested memory which must be dynamically allocated (grows upwards)
Stack frame
Every time a function call is made a new stack frame is created and memory allocated for variables to be used by the function
- if stack and heap continue to grow, all memory available to the process will be exhausted and out of memory error will occur
Reason for dynamic memory
If we don’t know how much memory will be used.
Then it would be inefficient to allocate as much memory as possible
Heap memory
For dynamic allocation when size of not known until run-time
- malloc()
Stack memory
When size is known at compile time
Anonymous variables
Memory addresses in the heap, do not have names
Malloc()
Free()
Memory leaks