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

An example Analysis of the use of Intelligent pointer in C++

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points of C++ 's intelligent pointer use example analysis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

What is RAII?

RAII (Resource Acquisition Is Initialization) is proposed by the father of C++. It is translated into Chinese for resource acquisition and initialization. The technology of using local objects to manage resources is called resource acquisition and initialization. The resources here mainly refer to the limited things in the operating system, such as memory (heap), network sockets, mutexes, file handles, etc., and the local object refers to the object stored on the stack, whose life cycle is managed by the operating system without human intervention.

The principle of RAII

The use of resources generally goes through three steps:

Get resources (create objects)

Use resourc

Destroy resources (destructed objects)

But the destruction of resources is often a link that programmers often forget, so the program community wants to make resources destroy automatically in the program. The solution to the problem is RAII, which makes full use of the automatic destruction of local objects in C++ language to control the life cycle of resources.

Problems with bare pointers

1. It is difficult to tell whether it points to a single object or an array

two。 After using the pointer, it is impossible to judge whether the pointer should be destroyed, because it is impossible to determine whether the pointer "owns" the object it points to.

3. When it has been determined that the pointer needs to be destroyed, it is not possible to determine whether to delete it with the delete keyword or other special destruction mechanisms, such as destroying the pointer by passing it to a specific destroy function.

4. Even if the method of destroying the pointer has been determined, it is still impossible to determine whether to use delete (to destroy a single object) or delete [] (to destroy an array) because of 1.

5. Assuming that all the above problems have been solved, it is difficult to guarantee that in all paths of the code (branch structure, challenges caused by exceptions), there is only one operation to destroy the pointer; omission of any path may lead to memory leakage. destroying multiple times can lead to undefined behavior

6. Theoretically, there is no way to tell whether a pointer is suspended or not.

Auto_ptrclass Object {int value;public: Object (int x = 0): value (x) {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