In C++, std::make_shared() is a function that returns a shared pointer to the object of specific type after dynamically creating it. It offers a safer and more efficient way to create shared pointers, reducing the chances of errors and improving performance. The function is declared in the <memory> header file.
In this use article, we will learn how to use std::make_shared() function in C++.
Syntax
std::make_shared<T> (args...)
Parameters
- T: Type of object which we want to create.
- args...: List of arguments for the constructor of object.
Return Value
Example of std::make_shared()
C++
// C++ Program to illustrate how std::make_shared
// function works
#include <iostream>
#include <memory>
using namespace std;
// Custom class for testing
class A {
public:
int data;
// Constructor to signal object creation
A(int val): data(val) {
cout << "Constructor called" << endl;
}
// Destructor to signal object deletion
~A() {
cout << "Destructor called" << endl;
}
};
int main() {
// Shared Pointer to the object of type A
shared_ptr<A> ptr = make_shared<A>(100);
cout << "Object data = "<< ptr->data << endl;
return 0;
}
OutputConstructor called
Object data = 100
Destructor called
We can also use the following syntax:
auto ptr = make_shared<T> (args..)
leaving the type deduction to the compiler.
Need of std::make_shared()
Shared pointer to any object can already be created as:
std::shared_ptr<T> ptr (new T(args));
So why a dedicated function whose only purpose is to create shared pointer was needed?
Actually, when we create a shared pointer to already existing object, there will be 3 different memory allocation. One for control block of shared pointer, one for the object and one for the shared pointer itself. But with std::make_shared(), there are only two memory allocations. One for shared pointer and another for both control block and the object. It results in better efficiency than the conventional shared pointer.
Create Shared Pointer to Array (Since C++ 20)
Since C++ 20, std::make_shared() function can also be used create a dynamic array of any particular type and return a shared pointer to it.
Syntax
std::make_shared<T[]> (size)
or
std::make_shared<T[size]>()
where, size is the number of elements in the dynamic array.
Example of Creating Array using make_shared()
C++
// C++ program to illustrate the creation
// of array using make_shared() function
#include <bits/stdc++.h>
using namespace std;
class A {
public:
A() {
cout << "Constructor Called" << endl;
}
~A() {
cout << "Destructor Called" << endl;
}
};
int main() {
// Creating an array of type A with 2 elements
auto sptr = make_shared<A[]>(2);
// Array will be automatically deallocated here
sptr = NULL;
return 0;
}
Output
Constructor Called
Constructor Called
Destructor Called
Destructor Called
Similar Reads
std::shared_mutex in C++ In C++, std::mutex is a mechanism that locks access to the shared resource when some other thread is working on it so that errors such as race conditions can be avoided and threads can be synchronized. But in some cases, several threads need to read the data from shared resources at the same time. H
4 min read
make_pair() in C++ STL In C++, make_pair() is a standard library function used to construct a key-value pair from the given arguments. The type of the pair constructed is deduced automatically from the type of arguments. In this article, we will learn about make_pair() function in C++.Letâs take a quick look at a simple e
3 min read
shared_ptr in C++ std::shared_ptr is one of the smart pointers introduced in C++11. Unlike a simple pointer, it has an associated control block that keeps track of the reference count for the managed object. This reference count is shared among all the copies of the shared_ptr instances pointing to the same object, e
5 min read
std::make_unique in C++ 14 std::make_unique is a utility function in C++ that was introduced in C++14. It is used to create a unique_ptr object, which is a smart pointer that manages the lifetime of dynamically allocated objects. It is defined inside <memory> header file. Syntaxstd::make_unique <object_type> (argu
2 min read
thread_local Storage in C++ 11 Thread local storage (TLS) is a feature introduced in C++ 11 that allows each thread in a multi-threaded program to have its own separate instance of a variable. In simple words, we can say that each thread can have its own independent instance of a variable. Each thread can access and modify its ow
3 min read
Memory Model in C++ 11 Memory Model is a specification that describes how the program interacts with the memory. In C++ 11, a standardized memory model is created to provide the solution to issues surrounding concurrency, ordering, and multithreading. This framework specifies how memory is accessed and arranged in a C++ p
5 min read