Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the use of C++ Intelligent pointer

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of C++ smart pointer use case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that after reading this C++ smart pointer use case analysis article will have something to gain, let's take a look.

1. Brief introduction

There are static space, stack and heap area when the program is running. We use heap to store objects that allocate space dynamically, that is, objects that allocate space while the program is running. If the object is no longer used, we must explicitly destroy them to avoid memory leaks.

Smart pointer is an object that can work like a pointer, there are unique_ptr (exclusive pointer), shared_ptr and weak_ptr and other smart pointers, defined in the header file, can manage dynamic resources.

Ensures that the constructed object will eventually be destroyed, that is, its destructor will eventually be called.

Note:

1. To avoid memory leaks, objects managed by smart pointers should have no other references to them.

two。 Smart pointers do not support arithmetic operations of pointers.

2.unique_ptr pointer (exclusive pointer)

What we use in most scenarios is unique_ptr.

By default, it is the same size as the ordinary pointer, without any additional consumption on memory, and has the best performance.

Note:

1. You cannot initialize a unique_ptr with the values of other unique_ptr objects. Nor can you assign one unique_ptr object to another. Such an operation will cause two exclusive pointers to share ownership of the same object.

2.unique_ptr represents exclusive ownership, if you want to hand over the memory of one unique_ptr to another unique_ptr object to manage.

You can only use std::move to transfer ownership of the current object. After the transfer, the current object no longer holds this memory, and the new object will gain exclusive ownership.

3. If unique_ptr points to an array of objects, be sure to call delete [] to handle the unallocated array, you should include a pair of empty square brackets [] after the object type.

# include#includeusing namespace std;int main () {unique_ptr up1 (new int (11)); cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report