
Explore the differences between unique_ptr and shared_ptr in C++11, including exclusive ownership, move semantics, and reference counting, and see how shared_ptr enables safe, multi-owner resource management.
Explore using raw pointers, shared_ptr, and unique_ptr to manage document resources in C++ 23 standard, highlight memory leaks, RAII, and why non-shared resources should use unique_ptr for efficiency.
Discover how shared pointers manage resources via a movable, copyable reference count that tracks copies, increments on copy, decrements on destruction, and deletes the resource when the count reaches zero.
Demonstrate copy semantics by replacing raw pointers with shared pointers to manage document ownership, eliminating leaks and double deletes through reference counting as the view and main share ownership.
Explore returning a shared_ptr by value instead of by reference, leveraging copy elision in C++17, to avoid dangling pointers and ensure safe shared ownership of a factory-created document.
Explore using shared pointers as arguments by contrasting raw pointers, implementing initialize, display, and serialize with null and empty checks, and addressing memory management and qualifiers.
Replace the document raw pointer with a shared_ptr to manage the resource, using reset or nullptr to release and leveraging reference-count behavior for by-value or by-reference use in multithreaded code.
Explore how shared pointers behave as function arguments, focusing on reference count changes when passing by value or by reference, and using move semantics to avoid copies, addressing thread safety.
See how uninitialized pointers from raw pointers cause runtime crashes and how shared pointers prevent these issues by default-initializing to null, avoiding delete errors and null checks.
Learn how shared_ptr prevents dangling pointers by automatically managing resource lifetimes and avoiding crashes and memory leaks when pointers are deleted or reset.
Learn how memory leaks occur and how smart pointers prevent them. See how exception safety and automatic destruction via shared_ptr prevent leaks when serialize throws in a try block.
Learn how shared pointers prevent memory leaks in exception scenarios by managing a document and buffer inside an application class, demonstrating strong exception guarantees when constructors throw.
Replace raw document pointers with shared pointers in a vector, and use a lambda comparator with sort and a predicate for uniqueness to ensure duplicate-free documents.
Build a polymorphic document hierarchy with virtual methods and a base pointer, manage memory to prevent leaks, and prepare for replacing raw pointers with shared pointers.
explains replacing raw document pointers with shared pointers in polymorphic code, enabling a vector of report or note pointers with static pointer cast and const pointer cast.
Demonstrate multithreading pitfalls when using pointers across threads while counting words in a document, and keep the interface responsive with a dedicated processing thread.
Explore multithreading with async and futures to process documents asynchronously, and resolve heap-use-after-free by replacing raw pointers with shared_ptr for safe cross-thread ownership.
Learn how shared pointers prevent uninitialized and dangling pointers, memory leaks, and double deletes, while enabling safe, polymorphic resource sharing and thread-safe usage.
Discover how a shared pointer uses a control block to manage ownership, including the deleter and use count, with copying increasing the count and deletion freeing the resource.
Explore reference counting with shared pointers and their use counts on a shared control block. Copying raises the count, and destruction lowers it to zero.
Explore how shared pointers use custom deleters to release resources, avoiding default delete, with examples using malloc and free, and understand type erasure and runtime behavior.
Explore how shared_ptr uses runtime deleter customization with malloc and free, resetting with new, and replacing deleters via lambdas or function objects, contrasting with unique_ptr's static deleter.
Learn to manage a C file pointer with shared_ptr and a custom deleter to call fclose, applying RAII to prevent file and memory leaks.
Open a Win32 file, read its content, and manage the handle with a shared pointer and a custom deleter to ensure automatic cleanup.
Manage Win32 atoms as resources with shared pointers and a custom deleter to prevent leaks, ensure exception safety, and safely create, access, and delete atom table entries.
Manage a dll handle with a shared pointer and a custom deleter to unload kernel32.dll via FreeLibrary, preventing leaks when invoking a beep function through GetProcAddress.
Manage a posix file descriptor with a shared_ptr and a custom deleter to ensure safe resource management, proper closing, and avoidance of leaks during file input/output.
Explore complex resource management by using a custom deleter with a shared_ptr to ensure close is called before deleting a file reader, illustrating robust raii and exception safety.
Explore how shared pointers manage deleters through a type-erased control block, including use and weak counters, atomic operations, and support for default and custom deleters.
Investigate the shared pointer internals, including the control block and refcount base, with a type-erased custom deleter invoked polymorphically to delete the managed resource when the count reaches zero.
Examine how shared_ptr deletes its resource with a type-erased, polymorphic deleter, and how the C runtime heap is shared across executable and dll when linked dynamically.
Export a factory from a dll to create a document, load the dll from executable, and allocate in the dll and delete in the executable with the same C runtime.
Demonstrates static and dynamic C runtimes creating module heaps and why cross-module deallocation yields undefined behavior. Shows how a client-deployed deleter and smart pointers enable memory via RAII.
Explore using unique_ptr across a dll boundary, employing a global function deleter and raii for the dll handle to ensure deletion occurs in the dll's heap and prevent crashes.
this lecture shows that a shared_ptr returned from a DLL uses a DLL-resident control block and deletes inside the DLL, even with a custom deleter, and cautions about cross-compiler risks.
Master the management of dynamic arrays with shared_ptr, including proper array delete, sfinae-enabled array support in C++17, and the new subscript operator for arrays.
Learn how to use a shared_ptr with a custom deleter to manage a dynamic array and ensure exception safety, compare to raw memory, and explain why vector may be preferred.
Explore how a shared pointer is constructed in MSVC, using global overloads of new and delete to log allocations, manage the resource and control block, and guarantee strong exception safety.
Compare conventional construction of shared_ptr under gcc and clang, highlighting Windows vs Linux behavior, control block allocation, exception safety, and delete calls.
Explore makeshared, a factory function that automatically allocates the resource and returns a shared pointer, ensuring strong exception safety and preventing explicit new for single objects and dynamic arrays.
Learn how std::make_shared simplifies shared_ptr creation, combining control block and resource in one allocation for faster construction and destruction, with strong exception guarantees and dynamic arrays support.
Understand how shared pointers allocate a control block with the managed resource, compare two allocations to one with make_shared, and outline a bare bones destruction-focused implementation.
Implement a control block for a resource using a union and placement new, enabling a make_shared-based shared pointer with a single memory allocation and proper destruction.
Examine the memory layout of the shared pointer control block and its ref counts. See that makes_shared stores the managed object inside the block for a single allocation.
Explore the concept of ownership in C++, distinguish unique and shared ownership, and learn how weak pointers enable observation without extending resource lifetime.
Explore shared ownership with shared pointers, learn how reference counts manage resource lifetimes, and prevent undefined behavior by ensuring observable access when a resource is freed.
Demonstrates managing a resource with shared_ptr inside a pretty printer, showing constructor/destructor logging, move and reset, and how two owners prevent release, introducing weak ownership for safety.
Understand weak ownership and weak pointers in c++, where a non-owner observes resources, use counts stay unchanged, and lock yields a temporary shared pointer to access the resource.
Explore how weak pointers observe a shared resource without owning it. Call lock to obtain a co-owner and observe use counts, then detect expiration when the owner is destroyed.
Replace the shared pointer with a weak pointer to achieve weak ownership, checking expiration and locking to access the resource only when available, so the resource can be destroyed safely.
Explore how the weak count in the shared pointer’s control block acts as a sentinel, allowing weak_ptr to observe state after resource destruction.
Understand how weak pointers increment the weak count without changing the use count, how copies raise the weak count, and how destroying pointers reduces counts to zero.
Acquire temporary ownership of a resource from a weak pointer with lock or direct shared pointer, and avoid the race-prone expired-check pattern in multithreaded code.
Acquire temporary ownership from a weak_ptr by calling lock to obtain a shared_ptr; if not expired you gain a co-owner, otherwise you receive an empty shared_ptr, thread-safe and idiomatic.
Acquire ownership from a weak pointer by constructing a shared pointer, and throw a bad weak pointer when expired if existence is a precondition, otherwise use the lock method.
Explore cyclic references in smart pointers, including how mutual dependencies form tight coupling and why they constitute an anti-pattern, with real-world examples like parent-child, window and widget, and manager-employee relationships.
Explores a window and button as a parent-child relationship, demonstrates memory leaks from raw pointers, and shows switching to shared_ptr to avoid leaks.
Uncover how interdependent shared pointers between the button and window prevent destructors from running, causing memory leaks. Track use counts to diagnose the lifecycle issue; the next lecture discusses fixes.
Break circular dependencies by replacing the button's shared pointer with a weak pointer to the window, clarifying ownership and preventing memory leaks.
Explore how make_shared optimizes shared_ptr creation while highlighting a memory-footprint trade-off with a large document object observed by weak_ptr, including the control block and logging of allocations and deletions.
Explore how make_shared allocates the document and its control block in one allocation, and how shared_ptr, weak_ptr, and observers govern destruction and memory lifetime.
Demonstrates how make_shared allocates and releases an array memory, and how a weak_ptr preserves the control block, preventing deallocation until the observer is destroyed, with memory profiling insights.
This course is a practical deep dive into modern C++ memory management through the lens of std::shared_ptr, designed to replace guesswork with clear mental models and repeatable patterns. Starting from basic usage, then copying, moving, resetting, and integrating shared_ptr into function arguments and return types—it builds toward fluency with containers, polymorphism, and multithreading so lifetimes remain correct even as designs grow more complex.
Along the way, lessons confront real bugs head‑on: uninitialized and dangling pointers, memory leaks, and subtle ownership mistakes that typically hide in large codebases. Then the course opens the hood to show how shared_ptr actually works: reference counts, control blocks, weak count, so behavior becomes predictable rather than mysterious. You’ll apply custom deleters to manage non‑memory resources cleanly — file pointers, file handles, dynamic linked library handles, and descriptors—so RAII consistently extends beyond heap objects.
Because performance and debuggability matter in production, the curriculum contrasts make_shared with direct new, explains allocation and layout implications, and highlights differences observed across MSVC, GCC, and Clang. Ownership patterns are the capstone: when and how to use shared ownership, where weak_ptr fits, how to model temporary borrowing safely, and how to break cycles in graphs, observers, and caches. The end result is confidence: the ability to read, design, and refactor code that is leak‑free, exception‑safe, and maintainable—backed by small, runnable examples that make each concept stick.