Modern C++ Smart Weak_ptr pointer and its uses

In this article, we are going to learn about one of the smart pointers in C++, the Weak pointers which we denote as Weak_ptr. We will learn what are Weak pointers, how we create Weak pointers and where we should use them, where we should not use them.

so let us begin with what are Weak pointers.

What are Weak_ptr in C++?

weak_ptr is a smart pointer that is kind of a subtype of shared_ptr smart pointer. This is because a weak_ptr holds a non-owing reference of a pointer that is owned by a shared_ptr. so it is kind of a temporary pointer that helps us access or track a shared_ptr. Creation of a weak_ptr from a shared_ptr does not increase the reference count of the shared_ptr.

so if the reference count of shared_ptr reaches 0, memory will be deleted inrespective of weak_ptr is still there or not.

weak_ptr Syntax :

The syntax to create a unique pointer is:
weak_ptr NameofPointer(new Data Type);
weak_ptr NameofPointer

weak_ptr : This is the syntax to specify that we are creating a weak_ptr of a specied Data Type. We are passing the Data
Types in the <> brackets. It can be a standard data type like int, float or a user defined data type like a class, structure etc.
NameofPointer : This is the Name of the pointer variable that you want to give it to. (new Data Type) : This is the memory allocation done by using the new operator and the data type so the proper size can be allocated on heap same as the size of data type.

C++ Program to demonstrate weak_ptr use

Now let us see this weak_ptr in some code examples. The first thing to keep in mind is we must include the <memory> header file to get the weak_ptr declaration in our program.

//Code Example using weak_ptr 
#include <iostream>
#include <memory>
using namespace std;

void Pointer_function()
	// Declaration of a shared_ptr of type int with a int raw pointer.
    shared_ptr<int> ptr(new int(63));
		cout<< " This is a shared_ptr pointer of type int";
	// Declaration of a weak_ptr which is empty.
    weak_ptr<int> second_ptr;
	if(second_ptr == nullptr)
		cout<< " This is a null or empty pointer of type int";
	second_ptr = ptr;
	cout << " The reference count is :" <<ptr.use_count();

int main()
    return 0;

How smart pointer weak_ptr is used

In this example code, we are first declarting a shared_ptr of type int and we are passing a raw pointer of type int as argument. This is make the reference count to 1. second declaration that we are doing is a weak_ptr, but this time we didn’t not pass any argument so it will create an empty weak_ptr which is not pointing to any memory.

At the end we are assigning pointer 1(shared_ptr) to pointer 2(weak_ptr). Now they both point to same memory location and share the data. This way we now have 2 pointers BUT the reference count is NOT increment to 2. The reference count will remain 1 and ownership of this pointer will remain with shared_ptr itself. Weak pointer will only get it to use. As soon as shared_ptr goes out of scope, the pointer will be deleted, does not matter if weak pointer is still in scope.

weak_ptr behaves the same way like a shared_ptr but with some limitations like reference count, and ownership.