Updated April 1, 2023
Introduction to C++ unique_ptr
A C++ unique_ptr is one of the types in smart pointer this pointer was implemented and developed in the C++ 11 version. It can be the replacement of the std::auto_ptr, when compared to the auto_ptr it’s more secure and it, will add n number of features for supporting the arrays unique_ptr is the container of the raw pointers. It supports only the one owner of the underlying pointers does not copy the assignments of the pointer that uniquely accepts the pointers after the initialization of the unique_ptr. It can be destroyed automatically and also the pointer resource is claimed if the duplicate unique_ptr is initialized means it creates compile-time errors.
Syntax:
In C++ each object, variables, keywords, and function have their own syntax and attributes for declaring in the programming codes. Based on the requirements we will utilize the special keywords, variables, data types, and functions from the programming libraries. The pointer is the address of the variable the unique_ptr is the unique one it does not support the duplicate copy of the pointers. The basic syntax for the unique_ptr type is below.
#include<iostream>
#include<memory>
using namespace std;
class classname{
Access modifier:
return type methodname()
{
---some C++ code logics---
}
return type main()
{
unique_ptr<classname> object name (new classname);
object name->method name();
----some coding logics based on the requirements---
}
How does the unique_ptr Function Work in C++?
The C++ programming has n number of reserved keywords, functions, and pointers that will provide some level of abstractions from the actual namespaces as well as the new libraries which are being already used for the programmers to allow it with more focus on the coding concepts. It also makes it easier to write the programming codes and clean it up using some methods like destroy() or any other default methods belonging to the garbage collections and it’s the main area for destroying the unwanted codes and cleaning up the memory space areas. It depends upon the data types and the object creation sizes must be calculated and it allocates the memory space for both the big storage data type variables as well as small amount storage variables. Normally the C++ declarations, initializations, and directives are used with some kind of memory spaces allocated for the functions to store it in the RAM. The function declaration and the definition will be the standard type of protocols and it brings all the types of members and the functions are calculated into the current and future scopes.
The pointers are the reference of the variable and the address is retrieved from the memory location. C++ has n number of pointers types like auto_ptr, unique_ptr, shared_ptr, and weak_ptr. When compared to other pointer types the unique_ptr is the unique one and it does not support the duplicate or copy the one pointer to another pointer type. I suppose we have to create the same copy of the unique pointers means it will throw the compile time errors. Also, the unique_ptr is moved by using the new move semantics that is it supports the move operation like std::move() function to transfer the contained pointer to another supported unique_ptr. It also a single pointer type of object and it will be used to reclaimed that the pointer is to be destroyed because the unique_ptr is the single resource ownership of the resources it means it can be pointed to one unique_ptr is called to one resource at a time. It is the probability of the unique-ptr when compared to the other pointer types.
Examples of C++ unique_ptr
Following are the examples as given below:
Example #1
Code:
#include <iostream>
#include <memory>
using namespace std;
class demo {
public:
void example()
{
cout<< "demo::example()" <<endl;
}
};
int main()
{
unique_ptr<demo>i(new demo);
i->example();
cout<<i.get() <<endl;
unique_ptr<demo> j = move(i);
j->example();
cout<<i.get() <<endl;
cout<<j.get() <<endl;
unique_ptr<demo> k = move(j);
k->example();
cout<<i.get() <<endl;
cout<<j.get() <<endl;
cout<<k.get() <<endl;
return 0;
}
Output:
Example #2
Code:
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
void demo(conststd::vector<int>& a)
{
for (auto i: a) {
std::cout<< ' ' <<i;
}
std::cout<< '\n';
}
class demo1 {
public:
void example()
{
cout<< "demo1::example()" <<endl;
}
};
int main ()
{
unique_ptr<demo1>l(new demo1);
l->example();
cout<<l.get() <<endl;
unique_ptr<demo1> m = move(l);
m->example();
cout<<l.get() <<endl;
cout<<m.get() <<endl;
unique_ptr<demo1> n = move(m);
n->example();
cout<<l.get() <<endl;
cout<<m.get() <<endl;
cout<<n.get() <<endl;
std::vector<int> a(4,113);
demo(a);
auto b = a.begin();
b = a.insert(b, 200);
demo(a);
a.insert(b,5,273);
demo(a);
b = a.begin();
std::vector<int> j(6,366);
a.insert(b+2, j.begin(), j.end());
demo(a);
int k[] = { 432,543,654 };
a.insert(a.begin(), k, k+4);
demo(a);
}
Output:
Example #3
Code:
#include <iostream>
#include <cmath>
#include <memory>
using namespace std;
class demo {
public:
void example()
{
cout<< "demo::example()" <<endl;
}
};
int main()
{
float m;
unique_ptr<demo>i(new demo);
i->example();
cout<<i.get() <<endl;
unique_ptr<demo> j = move(i);
j->example();
cout<<i.get() <<endl;
cout<<j.get() <<endl;
unique_ptr<demo> k = move(j);
k->example();
cout<<i.get() <<endl;
cout<<j.get() <<endl;
cout<<k.get() <<endl;
m = -67;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -676.5645;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = 7665.2456;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.67832;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.87892;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -6767.25245;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
m = -.6527;
cout<<"abs("<<m<<"): "<<abs(m)<<endl;
return 0;
}
Output:
Conclusion
The pointers are one of the main types of C++ programming codes it has smart pointers that can be owned and managed from one object to another object. Also, this unique_ptr is the dynamic memory management and it manages the single object so we can create the object using the new keyword. After the creation of the unique_ptr, we can destroy the pointer using the get_delete() (ptr) method for deleting the object reference in memory management and it will use to deallocates the memory in C++.
Recommended Articles
This is a guide to C++ unique_ptr. Here we also discuss the introduction and how does the unique_ptr function work in c++? along with different examples and its code implementation. You may also have a look at the following articles to learn more –