0Pricing
C++ Academy · Lesson

Why Smart Pointers Replace Raw new delete

Understand RAII and why owning raw pointers is rarely correct in modern C++.

Raw new and delete

The C inheritance: new T allocates a T on the heap; delete frees it. You must match every new with exactly one delete — or leak (or crash).

Widget* w = new Widget();
// ... use w ...
delete w;        // forget this and you leak

What Goes Wrong

Common failures:

  • Forgotten delete — memory leak
  • Double delete — undefined behavior
  • Exception thrown between new and delete — leak
  • Returning a raw pointer with unclear ownership

All lessons in this course

  1. Why Smart Pointers Replace Raw new delete
  2. unique_ptr Exclusive Ownership
  3. shared_ptr and weak_ptr Shared Ownership
  4. Custom Deleters and Make Functions
← Back to C++ Academy