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.
Table of Contents
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,
1 2 3 |
int *p; |
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:
1 2 3 |
template< class T > class auto_ptr; |
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:
1 2 3 |
template<> class auto_ptr<void>; |
Here is a auto_ptr example used with a struct,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <memory> //----------------------------------------------------------------------------- class Tmyclass { public: int X = 100; Tmyclass() { std::cout << "myobject constructed" << std::endl; } ~Tmyclass() { std::cout << "myobject deconstructed" << std::endl; } }; //----------------------------------------------------------------------------- void test_auto() { std::auto_ptr<Tmyclass> myobject( new Tmyclass); std::cout << "X = " << myobject->X << std::endl; } //----------------------------------------------------------------------------- int main() { test_auto(); getchar(); return 0; } |
Output of this code will be as below,
1 2 3 4 5 |
myobject constructed X = 100 myobject deconstructed |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition