Smart pointer, auto_ptr

Source: Internet
Author: User

If a pointer variable exists in the class, the memory of the pointer variable is dynamically allocated. There are two main problems.

(1) In terms of assignment, replication, and so on, we only talk about the mutual replication of pointer values, but the pointer pointing to the object is public. If an object is destructed, will delete the public pointer to the object, the other object will appear a suspension pointer.

Solution:

1) Deep replication: creates an object and assigns the same value each time.

2) reference count. The public object is deleted only when the object is 0.

(2) When a class object is dynamically created, this class contains pointer variables for dynamic memory allocation. If the code exception occurs before the delete operation, the dynamically allocated memory of this object will be leaked.

Therefore, a class encapsulation occurs. The RAII resource allocation is initialization, And the auto_ptr class is a specific application.

 

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1290444

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.