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

Reference count

Reference count in c++ helps program to keep track of pointer usage and delete it from memory when it is no longer needed.

You can read different methods to use reference counts on you program using smart pointers in following pages:

  1. Unique Pointer

  2. Shared Pointer

  3. Weak Pointer

You can check how to get count of reference in each of the smart pointer using below code snippet:

#include <iostream>
#include <memory>

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

int main() {
    // Create a shared pointer
    std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>();
    std::cout << "Reference count after ptr1 creation: " << ptr1.use_count() << std::endl;

    // Create another shared pointer pointing to the same object
    std::shared_ptr<MyClass> ptr2 = ptr1;
    std::cout << "Reference count after ptr2 creation: " << ptr1.use_count() << std::endl;

    // Create a weak pointer
    std::weak_ptr<MyClass> weak = ptr1;
    std::cout << "Reference count after weak ptr creation: " << ptr1.use_count() << std::endl;

    // Reset ptr2
    ptr2.reset();
    std::cout << "Reference count after ptr2 reset: " << ptr1.use_count() << std::endl;

    // Reset ptr1
    ptr1.reset();
    std::cout << "Reference count after ptr1 reset: " << (weak.expired() ? 0 : weak.lock().use_count()) << std::endl;

    return 0;
}

The output of this program would look something like this:

MyClass constructed
Reference count after ptr1 creation: 1
Reference count after ptr2 creation: 2
Reference count after weak ptr creation: 2
Reference count after ptr2 reset: 1
MyClass destructed
Reference count after ptr1 reset: 0

Key points in this example:

  1. We define a simple MyClass with a constructor and destructor that print messages when they're called.

  2. We use std::make_shared<MyClass>() to create a shared_ptr named ptr1.

  3. We use the use_count() member function to get the current reference count of the shared pointer.

  4. We create another shared pointer ptr2 that points to the same object as ptr1. This increases the reference count.

  5. We create a weak pointer weak from ptr1. This does not increase the reference count.

  6. We reset ptr2, which decreases the reference count.

  7. Finally, we reset ptr1. At this point, the object is destroyed because there are no more shared pointers pointing to it.

  8. For the last count, we use weak.expired() to check if the object still exists, and if it does, we use weak.lock().use_count() to get the count. If the object no longer exists, we print 0.

It's important to note that while use_count() is useful for understanding and debugging shared pointer behavior, you shouldn't rely on it for program logic, as its exact behavior can vary between implementations.

PreviousWeak PointerNextHelper function

Last updated 9 months ago

Run it .

🎯
here