01 / 22

new/delete &
Memory Risks

Learn how C++ gives you direct control over memory with new and delete, and discover why that power comes with serious risks.

C++

What is Memory?

Every variable you create in C++ needs a place to live while your program runs. Your computer's RAM (Random Access Memory) is that place: a massive grid of numbered bytes, each with a unique address.

0x00 0x04 0x08 0x0C 0x10 0x14 42 7.5 int x double y RAM (bytes with addresses)

Stack vs Heap

C++ divides memory into two main regions. Understanding their differences is the key to understanding new and delete.

Stack
Automatic memory. Variables are created when a function starts and destroyed when it ends. Fast, but limited in size.
Heap
Manual memory. You request it with new and must return it with delete. Large, but you manage its lifetime.

Why Use the Heap?

1
Data that outlives a function: stack variables vanish when the function returns. Heap data persists until you explicitly delete it.
2
Large data structures: the stack is typically only a few MB. The heap can use all available RAM.
3
Dynamic sizing: when you don't know at compile time how many elements you need (e.g., user input determines array size).

The new Operator

The new keyword allocates memory on the heap and returns a pointer to it.

// Allocate a single int on the heap int* p = new int; // p points to heap memory *p = 42; // store 42 there // Allocate an array of 5 doubles double* arr = new double[5];

The delete Operator

When you're done with heap memory, you must return it to the system using delete. Otherwise, the memory stays reserved forever (within your program's lifetime).

int* p = new int(42); // ... use *p ... delete p; // free single object double* arr = new double[5]; // ... use arr ... delete[] arr; // free array (note the [])

Memory Lifecycle

new Allocate memory Use Read/write *ptr delete Free memory What happens in memory: HEAP [free] Before new HEAP 42 After new HEAP [free] After delete Pointer p holds the address throughout

Heap vs Stack Memory

Heap (new/delete) Stack (automatic)
Created by new Declaration
Destroyed by delete (manual) Scope exit (auto)
Size limit All available RAM ~1-8 MB typical
Speed Slower (OS call) Very fast
Risk level High (leaks, dangling) Low (automatic)

How to Use new/delete Safely

1
Allocate with new and store the pointer immediately in a named variable.
2
Use the data through the pointer. Dereference with * or index with [] for arrays.
3
Delete the memory when done. Use delete for single objects, delete[] for arrays.
4
Set the pointer to nullptr after deleting. This prevents accidental use of a dangling pointer.
int* p = new int(99); // Step 1: allocate cout << *p; // Step 2: use (prints 99) delete p; // Step 3: free p = nullptr; // Step 4: safety net

Risk 1: Memory Leak

A memory leak occurs when you allocate heap memory but never delete it. The memory stays reserved but becomes unreachable: no pointer points to it anymore.

void leaky() { int* p = new int(42); // function returns without delete! } // p (stack) is destroyed, but the // heap int lives on — LEAKED
STACK p = 0x08 ✕ HEAP 42 p is destroyed when function ends Heap int remains but unreachable! MEMORY LEAK

Risk 2: Dangling Pointer

A dangling pointer is a pointer that still holds the address of memory that has already been freed. Using it causes undefined behavior: crashes, corrupted data, or silent bugs.

int* p = new int(10); delete p; // memory freed cout << *p; // DANGER: p still holds old address // but that memory is no longer yours!

Risk 3: Double Delete

Calling delete on the same pointer twice is undefined behavior. The memory manager's internal bookkeeping gets corrupted, often causing a crash.

int* p = new int(5); int* q = p; // q and p point to same memory delete p; // freed once — OK delete q; // freed AGAIN — CRASH!

Example: Single Object

// Create a single integer on the heap int* age = new int; *age = 17; cout << "Age: " << *age << endl; // prints 17 delete age; // free the memory age = nullptr; // safety: prevent dangling

Example: Dynamic Array

int n; cout << "How many scores? "; cin >> n; // Allocate array of size n at runtime double* scores = new double[n]; for (int i = 0; i < n; i++) cin >> scores[i]; // ... process scores ... delete[] scores; // free the ARRAY scores = nullptr;

Example: Spot the Bug

void process() { int* data = new int[100]; // ... fill and use data ... if (data[0] == 0) return; // BUG: early return, no delete! // more processing... delete[] data; }

Common Mistakes

✕ Forgetting delete
Every new must have a matching delete. If any execution path skips it, memory leaks.
✕ Mixing delete and delete[]
Using delete on an array (or delete[] on a single object) causes undefined behavior.
✕ Using after delete
Accessing memory through a pointer after delete is a dangling pointer bug. Set to nullptr immediately.

Memory Risks Summary

Risk Cause Prevention
Memory Leak Forgot delete Always pair new with delete
Dangling Pointer Used pointer after delete Set to nullptr after delete
Double Delete Two pointers to same memory nullptr check or single owner
Wrong Delete delete vs delete[] mismatch Match new[] with delete[]

Question 1 of 5

What does the new operator return?

Question 2 of 5

Which code correctly frees an array allocated with new int[10]?

Question 3 of 5

What happens if you call delete on a pointer, then dereference that same pointer without setting it to nullptr first?

Question 4 of 5

What memory risk does this code have?

Question 5 of 5

Why is it safe to call delete on a nullptr?