Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • ๐Ÿ›ฃ๏ธC Plus Plus: Docs
  • Build
    • ๐Ÿ”ฅBuild Process
    • ๐Ÿ”งConnect multiple C++ Files
  • Code
    • โฎ๏ธPre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • ๐Ÿช…Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • ๐ŸŽญCasting
    • ๐Ÿ”ƒOperator overloading
      • Available Operators
      • Examples of operator overloading
    • ๐Ÿ—บ๏ธNamespace
      • Namespace Example
      • Using directive
    • ๐Ÿ•ต๏ธHeader File
      • For C++ Classes
    • ๐Ÿ—๏ธStructure
      • Struct vs Class
      • Public vs Private inheritance
    • ๐ŸขClasses
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • ๐Ÿง Memory Introduction
    • โœจHeap and Stack
    • ๐ŸŽฏPointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • ๐Ÿ‘จโ€๐ŸญHelper function
    • ๐ŸกVector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • โ™ป๏ธUnion
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • ๐ŸงตThreading
      • std::thread
      • Detach a thread
  • Misc
    • ๐Ÿ—‚๏ธExecution Order
    • ๐Ÿง Print memory
Powered by GitBook
On this page
  1. Memory
  2. Pointers
  3. Smart Pointers

Shared Pointer

As we saw, unique pointer are one-to-one mapped. Meaning, one pointer variable can only stored by one unique_prt and it cannot be referenced by any other unique pointer. A unique pointer will free the referenced memory as soon as this unique_ptr goes out of scope.

A shared_ptr is different from that, here you can create multiple wrappers that are refer to same pointer, memory will be freed once all the shared_ptr goes out of scope.

Shared pointers are a type of smart pointer introduced in C++11 that provide automatic memory management through reference counting. Here's a concise overview:

Shared pointers (std::shared_ptr) allow multiple pointers to share ownership of a dynamically allocated object. Key points:

  1. Automatic memory management: The object is automatically deleted when the last shared pointer owning it is destroyed.

  2. Reference counting: Internally maintains a count of how many shared pointers are pointing to the object.

  3. Thread-safe reference count: The reference count is incremented and decremented in a thread-safe manner.

  4. Overhead: Slight performance overhead due to reference counting.

  5. Cyclic references: Can lead to memory leaks if not handled properly (use weak_ptr to break cycles).

#include <memory>
#include <iostream>

class MyClass {
public:
    MyClass() { std::cout << "MyClass constructed\n"; }
    ~MyClass() { std::cout << "MyClass destructed\n"; }
};

int main() {
    {
        std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>();
        {
            std::shared_ptr<MyClass> ptr2 = ptr1; // Share ownership
            std::cout << "Count: " << ptr1.use_count() << std::endl; // Prints 2
        }
        std::cout << "Count: " << ptr1.use_count() << std::endl; // Prints 1
    }
    // MyClass object is automatically deleted here
    return 0;
}
PreviousUnique PointerNextWeak Pointer

Last updated 9 months ago

๐ŸŽฏ