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 leakWhat Goes Wrong
Common failures:
- Forgotten
delete— memory leak - Double
delete— undefined behavior - Exception thrown between
newanddelete— leak - Returning a raw pointer with unclear ownership
All lessons in this course
- Why Smart Pointers Replace Raw new delete
- unique_ptr Exclusive Ownership
- shared_ptr and weak_ptr Shared Ownership
- Custom Deleters and Make Functions