FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses Pointers and Memory in C

First year

Progress0 / 6 lessons
  1. 1. What an address really is
  2. 2. Declaring and dereferencing pointers
  3. 3. Pointers and arrays are not the same thing
  4. 4. Passing by pointer to change a caller variable
  5. 5. malloc, free and what a memory leak costs
  6. 6. Dangling pointers, double free and real crashes

Courses › Pointers and Memory in C

Dangling pointers, double free and real crashes

The five memory bugs that actually crash student programs, and how to spot each one.

12 min read · Lesson 6 of 6 · Pro

Undefined behaviour, and why "it worked on my laptop"

When your C program breaks a rule — reads past an array, uses freed memory — the standard says the behaviour is undefined. Not "crashes". Not "prints garbage". Undefined: anything is allowed to happen, including working perfectly today and failing in the evaluation lab tomorrow.

That is why memory bugs are the hardest bugs in C. The symptom appears far from the cause, and it moves.

Here are the five you will actually meet.

1. Use after free (dangling pointer)

C
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int *p = malloc(sizeof(int));
    if (p == NULL) return 1;
The rest of this lesson is Pro

The free lessons of Pointers and Memory in C finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.