C Programming 07/8 Flashcards
What is Static Allocation?
Storage allocated statically when program starts, freed automatically when program ends.
• Variable defined with the static identifier in functions.
• Both static and non-static variables at the global scope.
What is Stack Allocation?
Memory automatically allocated when function is executed.
• Fixed size of memory automatically allocated at run- time.
• Used for variables that are local to a block
What is Dynamic Allocation?
Memory allocated at run-time.
• Done manually by the programmer.
• Lifetime of the memory is handled by the programmer.
What is malloc?
Allocates a new block of raw memory of certain size (in bytes)
• void *malloc(size_t size);
• Returns NULL if there is insufficient memory left in the OS.
• Memory returned has no type, and will be filled with random data
What is free?
Returns a block of memory to the OS.
• void free(void *ptr);
• There is no automatic memory management
• Undefined behaviour if you read or write from a pointer into a block of already freed memory.