C++ use_count()
The use_count() method in C++ is a member function of std::shared_ptr that returns the number of shared_ptr objects that share ownership of the same managed object. This function provides a way to check how many shared pointers are currently referencing the same resource, which is useful for debugging, resource management, and understanding object lifetime in shared ownership scenarios.
Syntax
long use_count() const noexcept;
Parameters:
This method takes no parameters.
Return value:
Returns a long integer representing the number of std::shared_ptr instances that share ownership of the managed object. If the shared_ptr is empty (does not manage any object), it returns 0.
Example 1: Basic Usage of use_count()
This example demonstrates the fundamental usage of use_count() to track shared ownership:
#include <iostream>#include <memory>int main() {// Create a shared_ptr managing an integerstd::shared_ptr<int> ptr1 = std::make_shared<int>(42);std::cout << "After creating ptr1: " << ptr1.use_count() << std::endl;// Create another shared_ptr sharing the same objectstd::shared_ptr<int> ptr2 = ptr1;std::cout << "After creating ptr2: " << ptr1.use_count() << std::endl;std::cout << "ptr2 use_count: " << ptr2.use_count() << std::endl;// Create a third shared_ptrstd::shared_ptr<int> ptr3 = ptr2;std::cout << "After creating ptr3: " << ptr1.use_count() << std::endl;return 0;}
This example creates multiple shared_ptr objects that share ownership of the same integer. Each time a new shared_ptr is created that shares the same object, the reference count increases. All shared_ptr instances sharing the same object will report the same use count.
The output of this code is:
After creating ptr1: 1After creating ptr2: 2ptr2 use_count: 2After creating ptr3: 3
Codebyte Example: Resource Management in Classes Using use_count()
This example shows how use_count() can be used in a real-world scenario for resource management within a class:
This example demonstrates a practical use case where use_count() helps determine whether it’s safe to modify shared resources. The ResourceManager class uses shared_ptr to manage a large vector, and use_count() helps determine if the resource is uniquely owned or shared.
Frequently Asked Questions
1. What is the count function in shared_ptr?
The count function in shared_ptr is use_count(), which returns the number of shared_ptr objects that share ownership of the same managed object.
2. How do you get the reference count in C++?
You can get the reference count of a shared_ptr by calling the use_count() method. For example: long count = my_shared_ptr.use_count();.
3. What is the use of a shared pointer in C++?
A shared pointer (std::shared_ptr) allows multiple pointers to share ownership of the same object. It automatically manages memory using reference counting and deletes the object when the last shared_ptr is destroyed.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours