C Memory Management
In this page:
#include <stdlib.h>
data_type *pointer_name = malloc(size_in_bytes);
free(pointer_name);
What is Memory Management?
Memory management in C is the process of allocating space for data as a program needs it and releasing that space when it's no longer needed, a responsibility the programmer handles manually rather than an automatic garbage collector.
Example: What is Memory Management?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 5;
// Print formatted text to the screen
printf("%d", *ptr);
// Release the heap memory so it can be reused
free(ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Stack vs Heap Memory
C programs use two main regions of memory: the stack, which automatically manages local variables tied to function calls, and the heap, a pool of memory that must be explicitly allocated and freed by the programmer.
Example: Stack vs Heap Memory
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Declare stackVar and set it to 5
int stackVar = 5;
// Allocate memory on the heap
int *heapVar = malloc(sizeof(int));
*heapVar = 10;
// Print formatted text to the screen
printf("%d %d", stackVar, *heapVar);
// Release the heap memory so it can be reused
free(heapVar);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static vs Dynamic Allocation
Static allocation, like a regular array declaration, fixes memory size at compile time, while dynamic allocation, using functions like malloc, lets a program decide how much memory it needs while it's actually running.
Example: Static vs Dynamic Allocation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
int staticArr[5];
// Allocate memory on the heap
int *dynamicArr = malloc(5 * sizeof(int));
// Print formatted text to the screen
printf("%zu %d", sizeof(staticArr), dynamicArr != NULL);
// Release the heap memory so it can be reused
free(dynamicArr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
The Allocate-Use-Free Lifecycle
Every block of dynamically allocated memory follows a lifecycle: it's allocated with a function like malloc, used through a pointer, and then released exactly once with free when it's no longer needed.
Example: The Allocate-Use-Free Lifecycle
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 42;
// Print formatted text to the screen
printf("%d", *ptr);
// Release the heap memory so it can be reused
free(ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Common Memory Management Mistakes
The most common memory management mistakes are memory leaks, where allocated memory is never freed, and use-after-free, where a pointer is dereferenced after its memory has already been released, both of which lead to serious bugs.
Example: Common Memory Management Mistakes
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 5;
// Release the heap memory so it can be reused
free(ptr);
ptr = NULL;
// Print formatted text to the screen
printf("%d", ptr == NULL);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Forgetting to
freememory obtained frommalloc, which causes a memory leak. - Using memory after it has been freed, which is undefined behavior.
- Returning the address of a local variable, which lives on the stack and is destroyed on return.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: