JS
C

JavaScript to C

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Pointers4Objects → Structs5Memory Management6Preprocessor & Headers7String Handling in Depth8Enums and Bitwise Operations9Bit-Fields and Unions10Multi-File Projects and Linking
All Mirror Courses
JS
C
Memory Management
MirrorLesson 5 of 10
Lesson 5

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.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with manual heap memory allocation.

C
In C:

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:

C
C 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:

JS
JavaScript (What you know)
// 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 free
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C requires explicit malloc/free; JS garbage collector handles memory automatically

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C memory leaks if you forget free; JS has no memory leaks from forgetting to free

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C use-after-free and double-free are undefined behavior; JS raises exceptions

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

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.

JS
JavaScript
const arr = new Array(1000); // GC handles cleanup
C
C
int *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.

Common Pitfall
Always set a pointer to NULL after freeing it to prevent accidental use-after-free.

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.

C
C
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
Common Pitfall
Don't assume C works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C to practice these concepts.
PreviousNext