Memory Management
Manual heap memory allocation
Introduction
In this lesson, you'll learn about memory management in C. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with manual heap memory allocation.
C has its own approach to manual heap memory allocation, which we'll explore step by step.
The C Way
Let's see how C handles this concept. Here's a typical example:
#include <stdlib.h>
#include <string.h>
/* Stack — automatic, freed on return */
int x = 42;
char buf[256];
/* Heap — manual */
int *arr = malloc(1000 * sizeof(int));
if (!arr) { exit(1); } /* check failure */
memset(arr, 0, 1000 * sizeof(int));
/* Use the memory */
arr[0] = 10;
/* MUST free or memory leaks */
free(arr);
arr = NULL; /* prevent dangling pointer use */
/* Struct on heap */
typedef struct { int x, y; } Point;
Point *p = malloc(sizeof(Point));
p->x = 1; p->y = 2;
free(p);Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
// JavaScript — automatic memory management
const arr = new Array(1000).fill(0);
const obj = { x: 1, y: 2 };
// Objects freed automatically when unreachable
function makeObj() {
return { data: new Array(100) }; // freed after return if not stored
}
// No memory leaks possible from forgetting to freeYou may be used to different syntax or behavior.
C requires explicit malloc/free; JS garbage collector handles memory automatically
You may be used to different syntax or behavior.
C memory leaks if you forget free; JS has no memory leaks from forgetting to free
You may be used to different syntax or behavior.
C use-after-free and double-free are undefined behavior; JS raises exceptions
You may be used to different syntax or behavior.
C malloc returns NULL on failure; must check before use
Step-by-Step Breakdown
1. malloc and free
malloc reserves heap memory; free releases it. Every malloc must have exactly one matching free.
const arr = new Array(1000); // GC handles cleanupint *arr = malloc(1000 * sizeof(int));
// ... use arr ...
free(arr); // mandatory!2. Common Memory Bugs
C memory bugs — leaks (forget free), double-free, use-after-free, buffer overflow — don't crash predictably and are hard to debug.
3. Stack vs Heap
Local variables live on the stack and are freed automatically when the function returns. Heap memory persists until you call free.
void example() {
int stack_var = 42; // freed on return
int *heap = malloc(sizeof(int)); // persists
*heap = 42;
free(heap); // must free before return
}Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- C requires explicit malloc/free; JS garbage collector handles memory automatically
- C memory leaks if you forget free; JS has no memory leaks from forgetting to free
- C use-after-free and double-free are undefined behavior; JS raises exceptions
Key Takeaways
- Every malloc needs exactly one free
- Memory leaks (forget free), double-free, use-after-free are common bugs
- Stack variables auto-freed; heap variables manual
- Always check malloc return for NULL