
Master unique pointers to replace memory management and prevent leaks and dangling pointers. Learn to migrate from raw pointers and use unique pointers in functions, returns, and object pools.
Source code
Explore dynamic memory allocation on the heap, including why runtime allocation matters, how memory persists beyond function scope, and how malloc, calloc, realloc, and free manage memory.
Use calloc to allocate an array and initialize its elements to zero, as shown with five integers. It zeros bytes but cannot set other values.
Realloc either expands the existing buffer or allocates a new one, shown by pointer addresses; store the return in a new pointer to avoid leaks if it fails.
Explore why C++ favors new over malloc, with new allocating memory from the free store, optional initialization, and delete freeing memory (destructors) for single objects and arrays.
Learn how the new operator allocates memory via operator new, initializes objects with constructors, and frees them with destructors on delete.
Explore how the new operator fails to allocate memory, handle bad_alloc with a custom new handler (lambda), and use the nothrow form to return null instead of throwing.
Explore the array form of new in C++, allocate dynamic memory for an array of five integers, initialize missing values to zero, and delete with the array delete.
Placement new initializes objects at a memory address without allocating memory; you must call the destructor explicitly, enabling embedded devices, memory pools, and custom allocators in the standard library.
Explore placement new in action: manually invoking destructors, using pre-allocated memory, and why destructors require explicit calls; learn how operator new and delete allocate raw memory.
Demonstrate manual reallocation in c++, since there is no realloc: allocate a larger array, copy old elements, delete old, and transfer ownership for the new array using product id objects.
Learn to implement efficient dynamic array reallocation using operator new and placement new to avoid extra assignments, and apply copy and move semantics, with proper destructors and memory management.
Examine how manual memory management with raw pointers causes null, dangling, and ownership risks, memory leaks, and auto_ptr issues; discover how smart pointers prevent these problems.
Explore using unique_ptr with a factory function to create a document, returning null when the title is empty and using move semantics and class template argument deduction for return types.
Explore using unique pointers as function arguments while comparing raw pointer handling, including initialization, display, serialization, and safe memory management to prevent leaks and dangling pointers.
Replace raw document pointers with unique_ptr, learn to initialize by reference, use move semantics for ownership, and employ reset or nullptr to release resources while avoiding leaks during serialize.
See how a unique pointer prevents uninitialized pointer issues by ensuring the document pointer is either null or valid when passed to display, avoiding crashes.
Identify how dangling pointers and double delete crash programs, then replace raw pointers with unique_ptr to prevent these issues and use reset or nullptr.
Learn how using unique_ptr ensures exception safety and prevents memory leaks during document serialization, replacing raw pointers and manual delete with safer, simpler code.
Explore how unique_ptr can work with containers, using a vector of document pointers and showing how memory leaks occur if pointers aren’t deleted, with a release function.
Learn how raw pointers in containers cause leaks and how the remove-erase idiom and erase_if simplify cleanup, then replace raw pointers with unique_ptr to ensure automatic destruction.
Discover how unique_ptr uses a deleter to release resources beyond memory, enabling RAII for handles such as socket handles or file handles with a custom or default deleter.
Discover how default constructible deleters work with unique_ptr, including function objects and stateless lambdas, and when to pass a deleter instance for non-default-constructible cases.
learn to use a custom deleter with unique_ptr to manage memory allocated with malloc, specifying the deleter type as a template argument and passing the deleter in the constructor.
Explore how to implement a custom deleter with a unique pointer to manage file resources, demonstrating reading a file with C APIs, memory cleanup, and safe resource management.
Manage Windows handles with a custom deleter in unique_ptr to automatically close file handles and unload DLLs, using get to access resources and ensure exception safety.
Demonstrates using a unique_ptr with a custom deleter to manage a Linux file descriptor, ensuring automatic close via RAII even though the resource is not a pointer.
Practice controlling object lifetime with a custom deleter for unique_ptr, using a factory create function, a tracking vector, and safe destruction that releases resources and updates the vector.
Explore how make_unique creates unique pointers on the heap with less verbosity, avoiding direct new and delete. It ensures exception safety by preventing leaks during argument evaluation.
Shows managing a heap-allocated dynamic array of documents with unique_ptr, uses raw pointers for indexing, reveals crash from the default deleter, and presents a custom deleter as a solution.
Discover using unique pointers with dynamic arrays through the array specialization, empty subscripts, and make_unique overloads, with sfinae-based selection and array-type deleters.
See how unique_ptr handles file resources with a custom deleter and how to build dynamic arrays as buffers while ensuring exception safety and no leaks.
Implement the pimpl pattern by wrapping the document class with a document impl and a nested forward declaration, preserving binary compatibility while planning a move to unique_ptr.
Explore object pooling with smart pointers to reduce dynamic memory allocation overhead, while addressing memory fragmentation, leaks, and dangling pointers in game scenarios.
Implement a generic object pool in c++ using a vector of pooled resources and a free list. Acquire and release resources; use raw pointers now, enforce the rule of five.
This comprehensive course provides an in-depth exploration of modern memory management in C++, with an emphasis on mastering unique_ptr. Designed for developers familiar with basic C++ syntax, the course bridges the gap between traditional manual memory allocation techniques and robust, exception-safe smart pointer usage that empowers modern C++ applications.
You will start with a foundational understanding of dynamic memory allocation, delving into functions like malloc, calloc, and realloc, and exposing common pitfalls, such as uninitialized memory bugs. Progress through modern C++ allocation using new, including its nuances, error handling, array management, and advanced placement techniques.
Explore the evolution from deprecated pointers such as auto_ptr to the type-safe and resource-managing smart pointers introduced in C++11. The heart of the course focuses on unique_ptr: its basic and advanced usage, function signatures, pass-by-value and pass-by-const-reference patterns, and application as parameter and return types. Uncover how unique_ptr prevents dangling pointers, boosts exception safety, integrates with containers, and leverages helper functions like make_unique.
Dive even further as you examine advanced idioms such as the PIMPL (Pointer to IMPLementation) pattern and custom deleters, illustrating how to manage resources with precision in real-world scenarios. Finally, you'll get to apply these skills by building and optimizing an object pool, both with and without pooling strategies.