Memory safety

Dangling pointers and use-after-free crashes.

The idea

In languages like C or C++, you manually manage the Heap memory. You ask the OS for memory (`malloc`), and you must return it (`free`).

If you free memory but keep a pointer looking at it, you have a Dangling Pointer. If you try to read from it later (a Use-After-Free bug), the program might crash, or worse—it might read garbage data that another part of the program just wrote there, leading to massive security vulnerabilities.

Ptr A Ptr B "Hello"
Ptr A is allocated on the heap pointing to "Hello".

How it works (Use-After-Free)

char* a = malloc(10);
strcpy(a, "Hello");

char* b = a; // b points to the EXACT SAME memory address as a

free(a); // Memory is returned to the OS.
// 'a' and 'b' still hold the physical memory address (dangling pointers!)

// LATER...
printf("%s", b); // USE AFTER FREE! 
// The OS might have re-assigned this memory to hold a password.
// Printing it leaks the password!