C + + Smart pointer template class review

Source: Internet
Author: User
Tags exit in

C + + Smart pointer template class review
#include <iostream>
#include <memory>
using namespace Std;
Smart pointers are used to ensure that programs do not have memory and resource leaks and are exceptionally secure.
C++98 provided auto_ptr,c++11 abandoned auto_ptr, and proposed unique_ptr, shared_ptr, weak_ptr


void Show1 ()
{
int* p = new int (4);
cout << *p << Endl;
}
void Show2 ()
{
int* p = new int (5);
Try
{
if (1)//here only make assumptions
{
Throw "Error";
}
}
catch (const char* ER)
{
cout << er << endl;
Return
}
cout << 1 << endl;
Delete p;
}
void Show3 ()
{
Auto_ptr<int> p (new int (5));
cout << *p << Endl;
}
Unique_ptr<int> get ()
{
Unique_ptr<int> p (new int (5));
return p;
}


int main ()
{
/*
The purpose of smart pointers
Show1 ();//show1 obviously caused a memory leak, although the pointer P is destroyed with the end of the Show1, but the open heap space remains
Show2 ();//show2 obviously if you encounter an error program exit in a try, it can also cause a memory leak
SHOW3 ();//auto_ptr is actually a template class instantiated with int, and the reclamation of the space is written in the destructor of the class, so with the destruction of the P object, the heap space also follows the destruction
*/


/*
When a pointer object between auto_ptr is assigned to each other, the ownership of the open space address is transferred, and if ownership is not transferred, the same heap memory is recycled multiple times, causing an error
Auto_ptr<int> Temp1 (new int (5));
cout << *temp1 << Endl;
Auto_ptr<int> Temp2 (TEMP1);//At this time Temp1 will open the first address of the heap space to Temp2, and Temp1 no longer have
cout << *temp1 << endl;//There will be an error, because Temp1 is already empty
*/


/*
Objects between shared_ptr do not transfer ownership when they are assigned to each other, because shared_ptr uses a counting mechanism, and when more than one smart pointer points to a heap space,
The destruction of a pointer object does not reclaim heap space, only when the count is 1 o'clock, only one pointer points to the heap space, and the heap space is reclaimed when the pointer object is destroyed
Shared_ptr<int> Temp3 (new int (5));
cout << *temp3 << Endl;
Shared_ptr<int> Temp4 (Temp3);
cout << *temp3 << endl;//no error at this time
*/


/*
Unique_ptr is similar to auto_ptr, but also establishes the concept of ownership, but it is not necessary to unique_ptr not allow the assignment between ordinary objects, otherwise it will be in the compile times wrong, but there is a special case, we next introduce
Unique_ptr<int> TEMP5 (new int (5));
cout << *temp5 << Endl;
Unique_ptr<int> temp6 (TEMP5);//This will be an error.


Unique_ptr<int> temp6 = Get (); This is the only special case, because the Get function destroys the original pointer immediately after P is returned, so there is no future error, so this assignment is allowed
cout << *temp6 << Endl;
*/


/*
Weak_ptr a special-case smart pointer used with shared_ptr. WEAK_PTR provides access to objects owned by one or more shared_ptr instances, but does not participate in reference counting.
If you want to observe an object but do not want it to remain active, use that instance. In some cases, you need to break the circular reference between shared_ptr instances.
Shared_ptr<int> TEMP7 (new int (5));
Weak_ptr<int> A (TEMP7); The Use_count member function is used to display the current count of shared_ptr pointers
cout << a.use_count () << Endl; Because A is an object, Use_count is the member function of a, so it is used.
Shared_ptr<int> TEMP8 (TEMP7);
cout << a.use_count () << Endl;
*/


return 0;
}

C + + Smart pointer template class review

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.