c++11-smart pointer and null pointer __c++

Source: Internet
Author: User

Nullptr

A null pointer is a pointer that does not point to valid data, previously expressed in 0, but this makes 0 both a pointer constant and an integer constant. C++11 still allows 0来 to represent a null pointer, so the expression nullptr==0 true, and using nullptr to represent a null pointer instead of 0 provides a higher type of security.


Smart pointers

If the pointer PS has a destructor, the destructor will release the memory it points to when PS expires. The problem, therefore, is that it is just a regular pointer, not a class object with destructors. If it is an object, you can have its destructor remove the pointing memory when the object expires.

Header file Memory

std::shared_ptr<string> PS (newstring);

Std::shared_ptr<string> PS (newstring ("a"));

All smart pointer classes have a explicit constructor

Keyword: in C + +, a constructor for a parameter (or a multiple-parameter constructor with a default value except for the first argument) takes two roles. 1 is a constructor, and 2 is a default and implied type conversion operator. In some cases (see the authoritative example below), it violates our (Programmer's) intent. It is time to add a explicit modifier to the constructor, specifying that the constructor can only be explicitly invoked/used and cannot be implicitly used as a type conversion operator.

Avoid two pointers pointing to the same object, preventing multiple deletions. OK:

1. Define the assignment operator so that it performs a deep copy so that the two pointers will point to different objects.

2, the concept of ownership, for a particular object, can only have a smart pointer can have it, so that only the owner of the object's smart pointer constructor will delete the object.

3, the creation of smarter pointers, tracking the number of smart pointers to a particular object, called reference count, assignment, Count plus 1, when the pointer expires, the count minus 1, only when the last pointer expires, only call delete.

AUTO_PTR<STRING>P1 (New String ("a"));

auto_ptr<string>p2;

P2 =p1;

In statement 3, when P2 takes over the ownership of a string object, P1 's ownership is stripped, and the benefit is to prevent P1 and P2 destructors from attempting to delete the same object, but P1 is dangerous if reused. The hanging pointer.

UNIQUE_PTR<STRING>P1 (New String ("a"));

unique_ptr<string>p2;

P2 =p1;

Statement 3 Here is illegal, avoiding the problem that P1 no longer point to valid data, so unique_ptr is more secure than auto_ptr (compile-time errors are more secure than potential program crashes).

Unique_ptr<string>demo (const char *s) {

Unique_ptr<string> Temp (newstring (s));

return temp;

}

unique_ptr<string>ps;

PS =demo ("a");

If Unique_ptr is a temporary right value, the compiler allows this, and if the source unique_ptr will exist for some time, the compiler will prohibit it.

C++11 has a standard library function std::move (), which moves the constructor and the right value reference.

PS2 =move (PS1);

There is another advantage compared to AUTO_PTR,UNIQUE_PTR, which has a variant that can be used for arrays

STD::UNIQUE_PTR<DOUBLE[]>PDA (new double (5));

However, you must use delete[] to remove it.

If your program uses multiple pointers to the same object, you should select shared_ptr. When you can use Auto_ptr, better choose Unique_ptr. The Boost library provides scoped_ptr similar to unique_ptr.

Related Article

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.