C++

Dynamically Allocated Array C++

Arrays are a very important data structure in C++ that serve the purpose of holding similar types of elements. Arrays are mainly divided into two different types i.e. static and dynamic. The static arrays are the ones whose size is pre-defined within the code whereas the dynamic arrays are the ones whose size is defined at the runtime. In this article, we will explicitly talk about the dynamic arrays in C++. Moreover, we will pay special attention to their allocation and deallocation by sharing a detailed example with you in Ubuntu 20.04.

Dynamically Allocated Arrays in C++

We have already stated that the size of a dynamic array is defined at the runtime. However, one might question why we need the dynamically allocated arrays in the first place when we can conveniently use the static arrays? Well, at times, you encounter such situations in which the size of an array is not known initially. In these cases, you can acquire the array size as input from the user at the runtime.

But this is not possible with the static arrays since the size of a static array once defined within a code cannot be changed. This is where the dynamically allocated arrays come into play that can define the arrays of any desired size at the runtime. The dynamic arrays in C++ can easily be created with the “new” keyword. The exact syntax will be clarified later in this article.

However, an important point to be noted over here is that the static arrays are always created on your system’s stack and your system itself takes the responsibility of freeing up its stack once your program terminates. On the other hand, the dynamically allocated arrays are always created on the heap and you have to manually free up the memory occupied by a dynamic array. Now, you need to see the example discussed below to understand the usage of the dynamically allocated arrays.

Using the Dynamically Allocated Arrays in C++ in Ubuntu 20.04

In this example, we want to teach you the usage of the dynamically allocated arrays in C++. We will tell you how you can declare and initialize a dynamic array at runtime. Then, we will display the elements of the dynamically allocated array. Finally, we will show you how you can deallocate the memory occupied by the dynamic array in C++. To learn all this, you will have to see the code shown in the following image:

In this C++ program, we have our “main()” function in which we have defined an integer “num”. This integer will correspond to the size of our dynamic array that we are going to create later. Then, we displayed a message on the terminal asking the user to enter any size of their choice for the dynamic array. After that, we took that size as input from the user. Then, with the help of the statement “int *array = new int (num)”, we declared a dynamic array at runtime which has the size equal to the “num” variable. “array” refers to the name of this array.

After that, we have displayed a message on the terminal again asking the user to enter the elements of that array. This message is followed by a “for” loop that iterates till the size of the array i.e. num. Within this loop, we took the elements of that dynamic array as input from the user.

Once the dynamic array was populated, we wanted to display its elements on the terminal for which we first displayed a message with the help of the “cout” statement. Then, we have another “for” loop which again iterates through the size of the dynamic array. Within this loop, we have simply displayed the array elements on the terminal. After that, we wanted to deallocate the memory occupied by this dynamic array for which we have used the “delete [] array” statement. Finally, for being on the safe side, we have used the “array = NULL” statement to also delete the NULL reference of the dynamic array whose memory we have just deallocated.

After writing this code, when we compiled and executed it, we were first asked to enter the size of the dynamic array. We wanted our dynamic array to be of size “5” as shown in the image below:

As soon as we entered the size of our dynamic array, we were asked to populate it. For doing so, we entered the numbers from 1 to 5 as shown in the following image:

As soon as we pressed the Enter key after populating our dynamic array, its elements were printed on the terminal. Moreover, the deallocation of the dynamic memory also took place because of which a notification regarding this also appeared on the terminal as shown in the image below:

Now, we will tweak the same code slightly. Up till now, we have learned how we can initialize a dynamic array in C++ and display its elements on the terminal. Although, we have also incorporated the code for deallocating this memory in our program, however, we are still not sure of whether the occupied dynamic memory has been deallocated successfully or not. To do this, we will try to access a part of that dynamic memory after deallocating it. If it is accessed successfully, then it will mean that the memory deallocation has not taken place correctly.

However, if we encounter any error message while accessing that memory after deallocating it, then that will mean that our occupied dynamic memory has now been deallocated successfully. You need to take a look at the following modified code to understand this:

In this modified C++ code, we have simply added a line at the end of our program i.e. cout<<array[2]. It means that we want to access the third index of our dynamic array after deallocating the memory occupied by it.

When we compiled and executed this code, it performed perfectly well, however, as soon as this last line was executed, an error was generated referring to a segmentation fault which in fact means that you are trying to access a memory location that no longer exists. This is displayed in the attached image.

It means that the deallocation of our dynamic array has taken place successfully. This output also implies that the attempt to access a memory location that no longer exists only results in a runtime error and not a compilation error. It means that such a code will always be compiled successfully and you will not be able to catch such an error until you actually run your code.

Conclusion

This article was aimed at teaching you the usage of the dynamically allocated arrays in C++ in Ubuntu 20.04. For that, we first highlighted the need of using the dynamically allocated arrays in C++. Then, we walked you through a detailed example that explained how you can create and deal with dynamic arrays in C++. Moreover, we also shared the method of deallocating the dynamic arrays. After going through this guide, you will surely get a basic understanding of dealing with dynamic arrays in C++.

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.