0Pricing
C++ Academy · Lesson

Common Memory Bugs

Avoid leaks and dangling pointers.

Common Memory Bugs is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Memory Leaks

A memory leak happens when heap memory is allocated but never freed. The program keeps consuming memory it can no longer reach.

void leak() {
    int* p = new int(5);
    // missing delete p;  -> leaked
}

Dangling Pointers

A dangling pointer points to memory that has already been freed. Dereferencing it is undefined behavior.

int* p = new int(7);
delete p;
// *p = 3;   // BUG: dangling access

Double Free

Calling delete twice on the same pointer corrupts the heap and often crashes the program.

int* p = new int(1);
delete p;
// delete p;   // BUG: double free

Use-After-Free

Reading or writing through a pointer after the object was deleted is a use-after-free — a common source of security vulnerabilities.

Buffer Overrun

Writing past the end of an array overwrites unrelated memory. Always stay within bounds.

int a[3] = {1, 2, 3};
// a[3] = 99;   // BUG: out of bounds

Mismatched delete

Using delete on a new[] allocation (or vice versa) is undefined behavior. Keep them paired.

int* arr = new int[4];
// delete arr;   // BUG: should be delete[] arr;
delete[] arr;     // correct

Uninitialized Pointers

An uninitialized pointer holds garbage. Always initialize to a valid address or nullptr.

#include <iostream>

int main() {
    int* p = nullptr;   // safe default
    if (!p) std::cout << "not set\n";
}

Returning Local Addresses

Returning the address of a stack local creates an instant dangling pointer, since the local dies on return.

int* bad() {
    int x = 10;
    return &x;   // BUG: x is gone
}

Smart Pointers Prevent Leaks

Replacing raw owners with std::unique_ptr eliminates most leaks and double frees: cleanup is automatic and single.

#include <iostream>
#include <memory>

int main() {
    auto p = std::make_unique<int>(5);
    std::cout << *p << "\n";
}   // freed exactly once, automatically

Tools to Catch Bugs

Tools like AddressSanitizer and Valgrind detect leaks, double frees, and out-of-bounds access at runtime. Make them part of your testing.

Prevention Beats Detection

The best fix is to avoid raw ownership entirely: use containers like std::vector and smart pointers so the library manages memory correctly for you.

Quick Check

Test your understanding of memory bugs.

Recap

You learned the classic memory bugs: leaks, dangling pointers, double free, use-after-free, buffer overruns, and mismatched delete. Prevent them with smart pointers and containers, and catch the rest with sanitizers and Valgrind.

Frequently asked questions

Is the “Common Memory Bugs” lesson free?

Yes — the full text of “Common Memory Bugs” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Common Memory Bugs”?

Avoid leaks and dangling pointers. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Common Memory Bugs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Stack Allocation
  2. Heap Allocation with new/delete
  3. Object Lifetime
  4. Common Memory Bugs
← Back to C++ Academy