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
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i
7 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so
9 min read
C++ Standard Template Library (STL) The C++ Standard Template Library (STL) is a set of template classes and functions that provides the implementation of common data structures and algorithms such as lists, stacks, arrays, sorting, searching, etc. It also provides the iterators and functors which makes it easier to work with algorith
9 min read
Map in C++ STL In C++, maps are associative containers that store data in the form of key value pairs sorted on the basis of keys. No two mapped values can have the same keys. By default, it stores data in ascending order of the keys, but this can be changes as per requirement.Example:C++#include <bits/stdc++.h
8 min read
C++ Data Types Data types specify the type of data that a variable can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared as every data type requires a different amount of memory.C++ supports a wide variety of data typ
7 min read