Open In App

auto_ptr in C++

Last Updated : 25 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, a memory leak may occur while de-allocating a pointer. So to ensure that the code is safe from memory leaks and exceptions, a special category of pointers was introduced in C++ which is known as Smart Pointers. In this article, we will discuss the auto pointer(auto_ptr) which is one of the smart pointers in C++.

Pre-Requisite: Pointer in C++, Smart Pointers in C++

Note: Auto Pointer was deprecared in C++11 and removed in C++17

Auto Pointer (auto_ptr) in C++

auto_ptr is a smart pointer that manages an object obtained via a new expression and deletes that object when auto_ptr itself is destroyed. Once the object is destroyed, it de-allocates the allocated memory. auto-ptr has ownership control over the object and it is based on the Exclusive Ownership Model, which says that a memory block can not be pointed by more than one pointer of the same type.

When an object is defined using auto_ptr, it stores a pointer to the allocated object and ensures that when the auto_ptr itself gets out of scope, the memory it is pointing to also gets destroyed.

 

Syntax of auto_ptr

The auto pointer in C++ is defined as:

auto_ptr <type> pointer_name = value;

Why do we need auto_ptr?

The aim of using auto_ptr was to prevent resource or memory leaks and exceptions in the code due to the use of raw pointers. Let’s see an example of a memory leak. Consider the following code:

C++




void memLeak() {
  classA *ptr = new classA();
  // some code
    
  delete ptr;
}


In this above code, we have used delete to deallocate the memory to avoid memory leaks. But what if an exception happens before reaching the delete statement? In this case, the memory will not be deallocated. Hence, there is a need for a pointer that can free the memory it is pointing to after the pointer itself gets destroyed.

The above example can be re-written using auto_ptr as :

C++




void memLeakPrevented() {
  auto_ptr<classA> ptr(new classA());
  // some code
}


The delete statement is no longer required while using auto_ptr.

Note: The arithmetic functions on pointers are not valid for auto_ptr such as increment & decrement operators.

Example of auto_ptr in C++

C++




// C++ program to illustrate the use of auto_ptr
#include <iostream>
#include <memory>
  
using namespace std;
  
// creating class with overloaded constructor and destructor
class Integer {
public:
    Integer() { cout << "Object Created" << endl; }
  
    ~Integer() { cout << "Object Destroyed" << endl; }
};
  
// driver code
int main()
{
    // creating auto pointer to Integar class
    auto_ptr<Integer> ptr(new Integer());
  
    // not using delete
  
    return 0;
}


Output

Object Created
Object Destroyed

In this example, we have created an auto_ptr object ptr and initialized it with a pointer to a dynamically allocated Integer object.

As ptr is a local automatic variable in main(), ptr is destroyed when main() terminates. The auto_ptr destructor forces a delete of the Integer object pointed to by ptr, which in turn calls the Integer class destructor. The memory that the Integer occupies is released. The Integer object will be deleted automatically when the auto_ptr object’s destructor gets called.

Why was auto_ptr removed?

auto_ptr was depreciated in C++ 11 and removed in C++ 17. The removal of the auto_ptr was due to the following limitations:

  1. An auto_ptr can’t be used to point to an array. While deleting the pointer to an array, we need to use delete[] but in auto_ptr we can only use delete.
  2. An auto_ptr can not be used with STL Containers because the containers, or algorithms manipulating them, might copy the stored elements. Copies of auto_ptrs aren’t equal because the original is set to NULL after being copied.
  3. The auto_ptr does not fit in the move semantics as they implement move by using copy operation.

Due to the above limitations, auto_ptr was removed from C++ and later replaced with unique_ptr.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads