C++C++11C++14C++17Introduction to C++Learn C++

Everything You Need To Use auto_ptr Smart Pointer in C++

Everything You Need To Use auto ptr Smart Pointer in C++

In computers, all data and operations during runtime are stored in the memory of our computers, IoTs, or in other microdevices. This memory is generally a RAM (Random Access Memory) that allows data items to be read or written in almost the same amount of time, irrespective of the physical location of data inside the memory.

This post will walk you through static and dynamic memory allocation, using c and c++ pointers; a video using pointers and memory addresses, using c++ smart pointers, and learning more about smart pointer auto_ptr. By learning how to use auto_ptr Smart Pointer in c++, it will help you to easily build C++ applications using the C++ Software.

Static Memory Allocation and Dynamic Memory Allocation

In programming, generally, there are two kinds of memory allocations, Static Memory Allocation, and Dynamic Memory Allocation.

Static Memory Allocation is a memory allocation method that is defined by variable definitions when programming and it has a fixed size, can not be changed during the run-time. These variables are defined variables in our programs like constants, strings, pointers, arrays. When a program is compiled, the compiler allocates part of the memory to store data. This is called Static Memory Allocation or Compile-time Memory. There are limitations in such static memory allocation to use these kinds of variables. Because:

These allocations are done in memory exclusively allocated to a program; we can’t increase the size of static allocations to handle more new elements. Thus, this may result in declaring larger arrays than required, which means a waste of memory usage. If we used less data than expected, Static Memory Allocations don’t allow us to reduce array size to save memory. It is hard to create advanced dynamic data structures which can be deleted, reallocated variables, linked lists, trees, and other data, which are essential in most real-life programming situations.

Dynamic Memory Allocation is a memory allocation method in which the memory is allocated during the execution of a program (at run-time). Dynamic Memory Management functions/methods involve the use of pointers and standard library functions. Sometimes we use pointers to point to the address of blocks of memory which is allocated dynamically. So we can easily access or operate on those dynamic memory allocations.

The C++ language is a great programming language with its ancestor C programming language. C programming language has both Static Memory Allocation and Dynamic Memory Allocation methods.

Using C and C++ pointers

Pointers are variables that hold addresses and the asterisk character ‘*’ is used to define pointers, they are used before the variable name. Pointers are some of the strongest aspects of the C & C++ programming languages. They allow you to reach any kind of type (including very long size bitmaps or videos or other data etc.) without copying the whole data.

For example, we can define a pointer for an integer variable as below,

Learn To Use Pointers and Memory Addresses video

Using C++ smart pointers


Allocating a memory and free them safely is really hard if you are programming a very big application. In Modern C++, there are smart pointers to avoid about forgetting to free the pointers. Smart pointers make easy to define pointers, they came with C++11 and most used are unique_ptr, auto_ptr, shared_ptr, weak_ptr. std::auto_ptr is deprecated in C++11 and removed in C++17. You may encounter legacy code which uses std::auto_ptr and this will give a good explanation of how it works but it should not be used in new code.

Smart Pointer auto_ptr

First, we should say that std::auto_ptr is deprecated in C++11 and removed in C++17, we use std::unique_ptr (Since C++11) instead of this old smart pointer. In this post we would like to explain this old smart pointer. The std::auto_ptr is a smart pointer that manages an object with a pointer, when the auto_ptr goes out of scope disposes of that object automatically. auto_ptr can be used to provide exception safety for;

  • the dynamically-allocated objects,
  • the passing ownership of dynamically-allocated objects into functions
  • the returning dynamically-allocated objects from functions.

Syntax:

If we copy an auto_ptr it copies the pointer and transfers ownership to the destination: Copy construction and copy assignment of auto_ptr modify their right hand arguments, and the “copy” is not equal to the original. std::unique_ptr is preferred for this and other uses, because of these unusual copy semantics of auto_ptr which may not be placed in standard containers.

Syntax:

Here is a auto_ptr example used with a struct,

Output of this code will be as below,

As you see here, we used a test() function that uses auto_ptr, construction method runs when defined. We can printout any properties of this object by using -> pointer indicator (i.e. myobject->X) and deconstruction method runs when test() function ends, which means myobject needs to be free, and disposes of myobject automatically.

In modern C++ we use std::unique_ptr (Since C++11) and other smart pointers, these Smart Pointers will be explained in the next posts.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond