The std::unique_ptr{} pointer

It should be clear by now that C++ provides an extensive set of APIs for allocating and deallocating dynamic memory. It should also be clear that whether you are using malloc()/free() or new()/delete(), errors are not only possible but likely in large applications. For example, you might forget to release memory back to the heap:

#include <iostream>int main(){    auto ptr = new int;    std::cout << ptr << '\n';}// > g++ -std=c++17 scratchpad.cpp; valgrind ./a.out// ==8627== LEAK SUMMARY:// ==8627== definitely lost: 4 bytes in 1 blocks// ==8627== indirectly lost: 0 bytes in 0 blocks// ==8627== possibly lost: 0 bytes in 0 blocks// ==8627== still reachable: 0 bytes in 0 blocks// ==8627== suppressed: 0 bytes in 0 blocks ...

Get Hands-On System Programming with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.