Smart pointers
My notes on C++ smart pointers: unique_ptr, shared_ptr and weak_ptr, how to pass them to functions and how weak_ptr breaks circular references.

There are three smart pointers in the standard library: unique_ptr, shared_ptr and weak_ptr. All of them came with C++11. The old std::auto_ptr was deprecated in C++11 and removed in C++17, so don’t use it.
The unique pointer is a class template that owns a pointer and deletes it when it goes out of scope. A unique pointer can’t be shared by other parts of the code, there is only one owner. To use any smart pointer you have to include the <memory> header in your program.
#include <iostream>
#include <cstdlib>
//memory file holds the code
#include <memory>
//this is a method to get a pointer
int* getPointer(int value){
int* p = new int{value};
return p;
}
//method of printing
void printOut(int* p){
if(!p){
return;
}
std::cout << *p << std::endl ;
}
void myMethod(int x){
std::unique_ptr<int> p{getPointer(x)};
//we have an object that holds the pointer
if (p == nullptr){
p.reset(new int{x});
//reset is the function which deletes
//the existing pointer memory space and
//takes the new one.
}
printOut(p.get());
//get returns actual pointer
p.reset(new int);
*p = 2;
//* is an overloaded operator which refers
//to the memory location of the pointer
//like the actual * operator.
printOut(p.get());
}
int main(){
myMethod(34);
return EXIT_SUCCESS;
}
So you may notice that it is not necessary to worry about the delete operation. That’s how the unique pointer solves our problem about memory management.
Update (2026): Since C++14 you can use
std::make_unique<int>(x)instead ofnew, andstd::make_unique<int[]>(n)for arrays. Same forstd::make_sharedbelow. C++20 also addedstd::make_sharedfor arrays andmake_unique_for_overwrite.
But you can’t copy a unique pointer. For instance, passing it to another method by value doesn’t compile, because the copy constructor is deleted. Of course you can use std::move, which calls the move constructor, to pass it. But keep in mind, if you move it, the ownership goes to the function and the old unique_ptr becomes empty (nullptr), so you can’t reach the value after the call.
But there is a solution to it. Pass a reference of the pointer instead of the pointer itself.
#include <iostream>
#include <cstdlib>
#include <memory>
void printOut(const std::unique_ptr<int> &p){
if(!p){
return;
}
std::cout << *p << std::endl ;
}
void myMethod(int x){
std::unique_ptr<int> p{new int{x}};
printOut(p);
// it uses the pointer itself
std::cout << *p << std::endl ;
// so we can reach it after the call
}
int main(){
myMethod(34);
return EXIT_SUCCESS;
}
If the function doesn’t care about ownership at all, passing a raw pointer with p.get() or a reference to the object (int&) is also fine, like in the first example.
On the other hand, the shared pointer is fine in many cases. Shared pointers hold a reference counter. It increases on every copy and decreases on every destruction. When the last copy is destroyed and the counter becomes 0, the shared pointer gives the memory back to the system.
So you don’t have to pass a reference of the pointer to a method, you can pass it by value. Use it when two or more objects really share the ownership of the same object. If one object simply “has a” another object, a member or a unique_ptr is usually enough.
#include <iostream>
#include <cstdlib>
#include <memory>
void printOut(std::shared_ptr<int> p){
if(!p){
return;
}
std::cout << *p << std::endl ;
}
void myMethod(int x){
std::shared_ptr<int> p{new int{x}};
printOut(p);
std::cout << *p << std::endl ;
}
int main(){
myMethod(34);
return EXIT_SUCCESS;
}
Weak Pointer
A shared pointer has two parts, the pointer and a control block with the counter. The weak pointer points to the same control block but doesn’t increase the strong counter, so it doesn’t keep the object alive. The weak pointer has two important methods.
expired returns a boolean value. If the shared pointer’s counter is 0, it’s expired.
lock returns a shared pointer to the object and increases its counter. If the object is already gone, it returns an empty shared pointer. Checking the result of lock is safer than calling expired first, because in multithreaded code the object can be destroyed between the two calls.
#include <iostream>
#include <memory>
class Printer{
std::weak_ptr<int> ptr;
public:
void setVal(std::weak_ptr<int> ptr){
this->ptr = ptr;
}
void doIt(){
auto sp = ptr.lock();
if(!sp){
std::cout << "Expired " << std::endl;
return;
}
std::cout << "Value is : " << *sp << std::endl ;
}
};
int main(){
Printer p;
std::shared_ptr<int> mypointer{new int};
p.setVal(mypointer);
//lets set the value to 11
*mypointer = 11;
//lets check
p.doIt();
}
With a circular reference, shared pointers cause a memory leak, because the counters never reach 0. We also use the weak pointer to avoid that.
#include <iostream>
#include <memory>
class A;
class B;
class A{
public:
/* shared pointer causes memory leak because it is circular
std::shared_ptr<B> b_ptr;
*/
//Avoid it
std::weak_ptr<B> b_weak_ptr;
A(){
std::cout << "Init A" << std::endl;
}
~A(){
std::cout << "Destroy A" << std::endl;
}
};
class B{
public:
/* shared pointer causes memory leak because it is circular
std::shared_ptr<A> a_ptr;
*/
//Avoid it
std::weak_ptr<A> a_weak_ptr;
B(){
std::cout << "Init B" << std::endl;
}
~B(){
std::cout << "Destroy B" << std::endl;
}
};
int main(){
std::shared_ptr<A> a{new A};
std::shared_ptr<B> b{new B};
/*
//lets make a circular reference
a->b_ptr = b;
b->a_ptr = a;
// It causes memory leak
*/
//without memory leak
a->b_weak_ptr = b;
b->a_weak_ptr = a;
}