Auto_ptr is a class template provided by the C + + standard library that helps programmers automate the management of individual objects that are dynamically allocated with the new expression, but there is no similar support for array management that is allocated with the new expression, and cannot store arrays with auto_ptr, and if so, the result is undefined.
The Auto_ptr object is initialized to point to the dynamic allocation object created by the new expression. When the lifetime of the Auto_ptr object is terminated, the dynamically allocated object is automatically freed.
Before you use the Auto_ptr class template, you must include the following header file:
#include <memory>
The Auto_ptr object is defined in the following three ways:
auto_ptr<type_pointed_to> identifier(ptr_allocated_by_new);
auto_ptr<type_pointed_to> identifier(auto_ptr_of_same_type);
auto_ptr<type_pointed_to> identifier;
Type_pointed_to represents the type of object created by the new expression. In the most common cases, we want to initialize the auto_ptr directly to the object address returned by the new expression. You can do this:
auto_ptr<int> Pi (new int (1024);
Pi is initialized to the address of the object created by the new expression, and the initialization value of the object is 1024. You can check the value of the object that auto_ptr refers to, in the same way as a normal pointer:
if (*pi!= 1024);
The object created by the new expression is pointed to by PI, which is automatically released when the PI lifetime ends. If pi is a global object, the object that pi points to is released at the end of the program.
If we initialize the Auto_ptr object with a class-type object, such as a standard string type, then:
Auto_ptr<string> Pstr_auto (New string ("Hello World");
Suppose you want to access a string operation now, and for a normal string pointer, do the following:
string* Pstr_type = new String ("Hello World");
Pstr_type->size ();
So, Auto_ptr is the same way:
Auto_ptr<string> Pstr_auto (New string ("Hello World");
Pstr_auto->size ();
The main motivation behind the Auto_ptr class template is to support the same syntax as the normal pointer type, but to provide automatic management for the release of objects referred to by the Auto_ptr object. At the same time, using Auto_ptr objects is no more expensive than using pointers directly.
In the following case, the Pstr_auto2 is initialized with the Pstr_auto value, and Pstr_auto's underlying object is string.
Auto_ptr<string> Pstr_auto2 (Pstr_auto);
Suppose you initialize another with a string pointer, such as:
string* pstr_type2 (Pstr_type);
So, both pointers hold a string address within the program's free store, and we have to be careful to apply the delete expression only on one pointer, while the AUTO_PTR class simulates supporting ownership concepts.
When auto_ptr is defined, it knows that it has ownership of the initialization string and has the responsibility to delete the string, which is the responsibility granted to the Auto_ptr object.
The question is, what happens to ownership when Pstr_auto2 is initialized to point to the same object as Pstr_auto? We don't want two auto_ptr objects to have the same underlying object-which can cause the problem of duplicate deletion, which is why we use Auto_ The PTR type is first to be prevented.