
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.
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.
Learn how the new operator allocates memory via operator new, initializes objects with constructors, and frees them with destructors on delete.
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.
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.
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 pointers as function arguments while comparing raw pointer handling, including initialization, display, serialization, and safe memory management to prevent leaks and dangling pointers.
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.
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.
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.
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.