MC logo
Automatic Pointer
[^] CSc 220 Home
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Automatically Deleting Pointer] [Classy Auto Pointer]
C++ doesn't have garbage collection, but it does have features designed to make managing deletion easier. One of these you know: The destructor.

The 1997 C++ standard introduced the auto_ptr template class. It is the sort of thing generally known as a “smart pointer,” which is a class that behaves much like a pointer but adds some automatic memory management.

Auto_ptr is by no means a general solution for memory management. It handles the simple case where an object is to be deleted when one specific pointer to it ceases to exist. It must be handled carefully, but it can simplify the problem of memory cleanup, especially in the presence of exceptions or early function returns.

Because of some shortcomings and easy misuse, auto_ptr has been deprecated in the 2011 C++ standard, in favor of the newer shared_ptr and unique_ptr classes. It's still useful, and will be with us for a while. Especially since support for the newer alternatives does not exist in all compilers.