C++

How to Allocate Memory C++

Whenever we want to deal with data, we need to allocate some memory to it to be stored over there and can be accessed anywhere anytime. Therefore, it is very important to understand the concept of memory allocation no matter which programming language you are dealing with. C++ also has some very extensive concepts associated with memory management and allocation. In this article, we will be giving you a brief overview of the methods of allocating memory in C++ in Ubuntu 20.04.

Memory Allocation in C++ in Ubuntu 20.04:

Memory can be allocated to different entities in C++, either statically or dynamically. By statically allocating memory, we essentially mean to assign the memory on the system’s stack, whereas by dynamically allocating memory, we intend to assign the memory on the system’s heap. The static memory is allocated at the compile-time, whereas the dynamic memory is allocated at the runtime. Moreover, the operating system handles the deallocation of the statically allocated memory, whereas the dynamically allocated memory must be handled manually by the programmer. Also, dynamic memory allocation is preferred when the memory size to be allocated is not known in advance.

However, when we specifically talk about memory allocation in C++, we generally mean the dynamic memory allocation since it needs to be dealt with carefully. It is so because the operating system does not take the responsibility of dynamic memory allocation, which is why the programmer himself/ herself has to do it intelligently. Since we know that the C++ programming language is a combination of different entities such as variables, pointers, arrays, objects, etc., the dynamic memory allocation is also divided into different types depending upon the diversity of these entities. In the following sections of this article, we will learn to allocate the memory dynamically in C++ in Ubuntu 20.04.

Methods of Allocating Memory in C++ in Ubuntu 20.04:

The dynamic memory allocation in C++ can broadly be classified into three different methods. These three methods of dynamic memory allocation in C++ have been explained in-depth below:

Method # 1: Memory Allocation of Pointers in C++:

The memory for the pointers in C++ can also be allocated dynamically. For teaching you the method of doing so, we have written a small C++ code shown in the following image:

In this program, we have a “main()” function in which we have declared a “float” type pointer named “test”. We have initialized this pointer to “NULL” at the beginning so that if this pointer contains any garbage values, they can easily be flushed out. After that, we have equalized this pointer to a “new float”. In this step, the dynamic memory allocation will take place during the execution of this C++ program. Then, we have assigned the value of “24.43” to this pointer to store this value at the allocated address. Then, we wanted to print this value on the terminal. Since we have allocated the memory dynamically to this pointer, we have to free it up manually at the end of our program. Because of this, we have used the “delete test” statement at the end of our program.

Now, to compile this program, we have used the command given below:

$ g++ AllocateMemory.cpp –o AllocateMemory

Afterward, we have executed this program with the following command:

$ ./AllocateMemory

When we executed this program, the value stored at the dynamically allocated location for our pointer was printed on the terminal as revealed in the appended image:

Method # 2: Memory Allocation of Arrays in C++:

In the same manner, the memory for arrays can also be allocated dynamically in C++. For teaching you the method of doing so, we have written a small C++ code shown in the following image:

In this program, we have a “main()” function in which we have declared an “integer” type variable “size” to store the size of the dynamic array. Then, we have printed a message on the terminal to enter the size of this array. After that, we have taken this size as input from the user. Then, we have declared an array and dynamically allocated memory to it using the statements “int *arr = NULL” and “arr = new int[size]”. Then, we wanted to take the elements of that array as input from the user, for which we have used a “for” loop. After that, we wanted to print all these values on the terminal, for which we have used another “for” loop. Again, since we have allocated the memory dynamically to this array, we have to free it up manually at the end of our program. Because of this, we have used the “delete [] arr” statement at the end of our program.

When we executed this program, we were first prompted to enter the size of our array, as shown in the image below:

After that, we were requested to enter the elements of that array as revealed in the appended image:

Finally, those elements were printed on the terminal as shown in the image below:

Method # 3: Memory Allocation of Objects in C++:

Similarly, the memory for the objects of a class can also be allocated dynamically in C++. For teaching you the method of doing so, we have written a small C++ code shown in the following image:

In this program, we have first created a class named “Sample”. We only have two public member functions within this class, i.e., one is the constructor, and the other is the destructor. In both of these member functions, we have printed a message on the terminal. After that, we have our “main()” function in which we have created a dynamic array of the objects of the “Sample” class. According to the size of this array, the constructor and destructor of this class will be called. Then, since we have allocated the memory dynamically to this array of objects, we have to free it up manually at the end of our program. Because of this, we have used the “delete [] sampleArray” statement at the end of our program.

When we executed this program, both the constructor and destructor of the “Sample” class were called twice because the size of the array of objects was “2,” as shown in the image below:

Conclusion:

This article aimed at discussing the methods of allocating memory in C++ in Ubuntu 20.04. We first talked about the two ways in which memory is allocated in C++, i.e., statically and dynamically; however, for the scope of this particular article, we were more interested in exploring the concept of dynamic memory allocation in C++. Therefore, we shared the three different methods in which the dynamic memory can be allocated in C++. Once you go through these examples, you will easily deal with memory allocation and deallocation in C++ in Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.