C + + Primer Learning Summary 12th Chapter dynamic Memory

Source: Internet
Author: User

12th Chapter Dynamic Memory


1. Apply for and use shared_ptr. P400

2. shared_ptr count. P402

assigning, copying, passing a smart pointer to a function, or returning a smart pointer to a function will increase the current count of smart pointers.

3. Examples of sharing data between different objects using smart pointers.

4. Use new to dynamically request memory. P407

By default, the memory objects that are requested by new are initialized by default.

5. The const object for the new application must be initialized. P408

If you request a built-in type, you must initialize it with parentheses (). If the class type is requested, it can be initialized by default if the class object has a default constructor. However, if the class object does not have a default constructor , It must be initialized with a different constructor .

6. Use Delete. P409

Delete You can only delete the pointer and the pointer must have been used New the requested memory space .

7. shared_ptr and new are used together. P412

8. Do not mix smart pointers and built-in pointers. P 413

9. Unique_ptr object initialization. P417

Because Unique_ptr "owns" its object, it cannot copy and assign values, and it can only be initialized with a built-in pointer through parentheses ().

But we can copy or assign a unique_ptr that is about to be consumed. As shown in the following function:

The reset () function and the release () function usage of the Unique_ptr object. P418

One by one. Unique_ptr pass its own undelete: P419

shared_ptr passing its own undelete is also similar to unique_ptr.

Weak_ptr Use Example P420.

The dynamic array is assigned to new. P424

Either mode 1 or Mode 2, you get a pointer to an array element type instead of an array type , so if you use begin or end for it is wrong

14. Use smart pointers to manage dynamic arrays. P425

It is recommended to use UNIQUE_PTR to manage arrays.



C + + Primer Learning Summary 12th Chapter dynamic Memory

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.