← Back to C Course | Chapter 9: Memory Management | Lesson 1 of 8

C Memory Management

Memory management in C is the process of manually allocating memory as it's needed and releasing it when it's no longer needed, following an allocate-use-free lifecycle to avoid leaks and undefined behavior.
Syntax
c
#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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to free memory obtained from malloc, which causes a memory leak.
  2. Using memory after it has been freed, which is undefined behavior.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.