Auto

zhaozj2021-02-12  171

AUTO_PTR_REF wonderful (on)

Auto_ptr is the only smart pointer in the current C standard, mainly to automatically manage the memory resources pointed to by the pointer. Resource management is a very important part of programming. Resource is a broad concept in your computer, which means that the number is limited in the program, things that must be returned, such as common mutex locks, file pointers, Win32 paintings (Brush ) ..., where memory is the most common resources, and we are first in contact with the most resources, so its status is critical. What is important? There are two distinct ideals:

1. Memory resources are so important that we cannot manage them to computers, but must be managed by programmers.

2. Memory resources are so important that we cannot handle them to programmers to manage, but must be managed by your computer.

Java, C #, Eiffel adheres to the second idea, gives memory resources to computer management, avoiding a large number of errors that programs in memory management, improve development efficiency, but will perform time with loss The efficiency is at the expense. Therefore, these languages ​​are rarely used in real-time systems, operating systems, language compilers, etc.

C language adheres to the first idea, C also inherits this concept, so that the memory resource management is handed over to programmers, and for high segment C programmers, it is of course giving them more flexibility, Very beautiful artwork can be prepared, but for beginners and less high-segment C programmers, memory management is the root of the trouble, bringing them more not flexibility, but a lingering servant . For example, some extremely difficult BUG, ​​which caused a Memory Leak, Wild Pointer, and the like, the final debug-in error may make us feel that the end of the world is here. [Note 1]

Note 1: Don't there be a joke? The real programmer uses C (or C ), I think, the difficulty is highly difficult, and the memory management responsible by the programmer himself may be an important reason to support this view. :)

However, in C , another management memory (even resource) is selected, that is, the smart pointer, the resource acquiring, the resource acquisition is initialization concept. [Note 2]

Note 2: In C 's quasi-standard Boost library, several new smart pointers scoped_ptr, scoped_array, shared_ptr, shared_array, weak_ptr, have many benefits of their benefits relative to Auto_PTR, interested readers can go to www.boost.org Go and see. Andrei Alexandrescu also implemented a scaled SmartPTR template in his Loki library, and some of the technologies used in http://sourceforge.net/projects/loki-lib/ Download the Loki library.

The introduction of Auto_PTR in the standard is to solve the memory resource management. This is an uncomfortable monster. However, while solving a problem, it will also bring some new problems, and Auto_PTR itself has the transfer of ownership, and it does not support "Value Semantic" concept [Note 3], Therefore, it cannot be used as an element in the STL container. In the new compiler, if such is used, the compiler will prevent you. But in a slightly old compiler, it is likely that there is no wind-free compilation. In the process of execution, then I congratulations, when you are baked in a very long mine, you can pass nonsense The probability that only knows. :) Note 3: About these concepts, you can refer to the "paradigi 2D plane".

Auto_PTR itself has a detailed explanation and examples in many books, and we can also read the source code of Auto_PTR to get more intuitive feelings. So I don't want to waste time and pen in the use of auto_ptr, I will see a strange type template auto_ptr_ref, I first read "The C Standard Library" time, see When I explain Auto_PTR, I mentioned that auto_ptr_ref will not understand, tell the truth, this book is very clear and easy to understand, but I think the pen ink in the place of auto_ptr is more embarrassed, not completely explained clear. And I have a lot of books, there is no detailed explanation of this auto_ptr_ref on the article. Today I want to discuss this, I hope to be able to throw the jade.

Note 4: There are some bugs in the early implementation of Auto_PTR, and later Nicolai M. Josuttis corrected this, and made an implementation. The reader can view it at http://www.josuttis.com/libbook/util/autoptr.hpp.html. The code itself is not long, I will lead them at the bottom. Readers should look at the article in accordance with the code. Among them, I have not explained the member function template, there are many books, mainly in order to solve the transformation between the pointers, especially for polymorphic pointers.

Auto_PTR_REF is like its name, transforming a value into reference (refer), that is, transforming a right value (rvalue) to a left value. [Note 5] We may be very strange, when will C still need this feature? Unfortunately, this feature needs to be used in order to ensure the correct behavior of Auto_PTR and certain security.

Note 5: What is the left value? What is the right value? There are many confusing concepts, and I will show my view in the next article.

We all know that Auto_PTR is transferred when the replication operation is a copy operation (Ownership), that is, the original auto_ptr pointing to the memory resources to the new auto_ptr, it is already air. So, auto_ptr does not support "value semantics", that is, the value replicated will not change. An example may be more description:

Auto_PTR ap1 (new int (9));

Auto_PTR ap2 (ap1); // AP1 lost ownership, now pointing to empty, AP2 now gets 9 // memory resources ap1 = ap2; // AP2 lost ownership, now pointing to empty, ap1 now pointing 9 Memory resources

When we observe Auto_PTR's Assignment Operator, you can also find that its parameters are auto_ptr & rhs, not Auto_Ptr Const & RHS [Note 6]. That is, when Auto_PTR performs replication operation, its quotes must be a left value that can be changed (the fact is that this value must be changed).

Note 6: This kind of writing, you may not be used to, in fact, const auto_ptr & rhs, why should I use this way to write, I can refer to I wrote "C, I'm", I know that I am not a new seismic. :)

We are most commonly seen, the parameter type of the copy operation is referenced to the constant, which is exactly to avoid changing the amount of arrival (argument). Since the referenced value is not changed, the C standard specifies that the constant reference can be referenced. For example, the following code is legal:

INT const & ri = 60; // 60 is right value

List const & rl = list (); // list () is right value

INT fun () {...}; int covest & rf = fun (); // fun () is right value

But generally reference (very quoted) is absolutely not to reference right. For example, the following code is illegal:

INT & RI = 60;

List & rl = list ();

Int fun () {...}; int & rf = fun ();

(To be contract!)

Wu Tong wrote in 2003.6.16

Recently modified 2003.6.16

转载请注明原文地址:https://www.9cbs.com/read-6147.html

New Post(0)