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

How to understand C++ memory Management

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

Share

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

In this issue, Xiaobian will bring you about how to understand C++ memory management. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

Foreword;

C++ inherits the pointer of C language. Some problems of pointer have been puzzling developers all the time. Common pointer problems mainly include: memory leakage, wild pointer, access out of bounds, etc. Fortunately, the C++ standards committee provided us with auto_ptr smart pointers, followed by the introduction of share_ptr and weak_ptr to help us use pointers correctly and safely. The following is mainly to introduce the solutions provided by the boost library.

1 smart_ptr Overview

In actual development, we will apply for different resources according to different programming scenarios. For the management of these resources, we need a perfect solution. We hope that after the release of resources, C++ can be like java and c#without manually releasing resources.

1.1 RAII

C++ programming usually uses this method to manage resources. After the application of resources exceeds the life cycle, the life object automatically calls the destructor function to recover the resources correctly. So it seems to be a perfect solution to our problem, at least not to manually release resources when using them. However, this method of resource release also has defects. If the object is created on the stack, the destructor function will be called automatically, and the result will be no problem. But what if the object is created on the heap by new? The result is that the destructor cannot be called automatically, and we also need to use delete for display destructor. There is also a risk of memory leaks if the program executes without calling destructor delete to free resources.

New/delete programming must follow the pairing principle and obey the rules of who creates who releases, which should be taken seriously by both novice programmers and experienced programmers.

1.2 smart pointer

Since C98, the C++ standards committee has provided us with smart pointers: auto_ptr. It partially solves the problem of automatic release of resources.

The method of use is as follows:

std::auto_ptr p (new int);

The constructor of auto_ptr supports the new operator or object pointers created by object factories as arguments. Object hosts the original pointer as soon as it is created, so it can return pointer objects using the get method, such as:

*p.get() = 100;

Auto_ptr has been welcomed by everyone, more and more people use this technology to solve most of the problems of resource management in actual programming, but auto_ptr is not a very perfect technology, nor does it cover all areas of smart pointers, especially reference-counting smart pointers. Similarly, when using auto_ptr, pay attention to the following points to avoid abuse of auto_ptr.

Auto_ptr cannot share ownership, i.e. do not let two auto_ptr point to the same object.

Auto_ptr cannot point to arrays because auto_ptr only calls delete when destructing.

Auto_ptr is just a simple smart pointer, if there are special needs, you need to use other smart pointers, such as share_ptr.

Auto_ptr cannot be used as a container object.

To address the shortcomings of auto_ptr, the boost library provides multiple classes to complement auto_ptr. These pointers are in the boost library header file,

As follows:

#include using namespace boost;1.3 scoped_ptr

This type pointer is similar to auto_ptr, but the restrictions are stricter. Once the scoped_ptr object obtains the management right of the object, it will always occupy it and cannot transfer the management right again. scoped_ptr, like its name, can only be used in scope. This makes the code relatively simple and does not add redundant operations.

scoped_ptr How to use:

scoped_ptr is easy to use, just replace it with scoped_ptr where new is used.

The method of use is as follows:

scoped_ptr pStr(new string("hello word"));

Operation scoped_ptr pointer is also very simple, using the same way as ordinary pointers, such as:

//print pointer points to string content 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