When we initialized the new array, the dynamic memory allocation accrued which puts variables on heap memory. This captures a long memory as you define an array in the code from the heap. There is a void return type inside the delete operator which does not return some value to the function. Delete[] is an operator whose function is to eliminate an array that is created by new expressions.
Need for Deletion of the Object
In programming languages, memory is the core point. We have to get as low memory as we can. To avoid memory loss when an array is created statically or dynamically using a new one or simply initialized, it captures memory in the Heap Section. If the things are not removed at that time, the code will crack for the period of execution. Once the array gets removed, all the elements of the array are also removed from the memory. The code in the below examples will help you understand this logic.
In this article, let’s discuss different approaches to deleting the array from the memory with the help of a very simple method in C++.
Deleting Empty Array Objects
Deleting arrays in all languages is the solo instant of the object in the code that could be made at the beginning of the program. Also at runtime by the use of an operator with the term “new” operator is removed by the delete operator. On the other hand, the array of objects is removed using the delete[] operator after that it cannot lead to a memory outflow. Here, we use a simple and basic example of the deletion of the array with the help of [] brackets but first, we use the array limit of 10 and then delete the array.
First, the header file ‘#include <iostream> is used. In the main body, we take an array with the name of “myarray” that is empty. Then we show the messages. After that, we write the delete [] operator and pass the “myarray” to it. The array is deleted from the memory successfully.
Because the array is empty and there is no element in the array, so we did not display the array. The elements will be shown in the next illustration.
Deleting Array Object Having Values
It is confirmed that every element of the array is deleted when you delete an array using the delete[] operator either it is empty or it is full. There is a universal rule that is you have to remove accurately those objects that you allocated with the new operator. Here, we see the example of the deletion of the array having values and not an empty array. For every type of array, the function is the same to delete the array.
In the main body, we take a variable ‘i’ of the loop and an array with the name of “arr” and length of 5 having elements 1 to 5. Then, we wrote a “for” loop to show the elements of the array to check whether our array is empty or not. After that, the delete[] operator performs its functionality. This is how our array is deleted.
Here you can easily see the elements of the array that was initialized at the start of the program.
Deleting Multiple Arrays
At last, here we are explaining how to delete two or more arrays in a single program. A variable whose data type is defined by the user contains actual data, it does not pointer to the data. In all programming languages, the pointers are also the variables but they have the addresses of other additional variables. Here, a* is a pointer variable while array1 and array2 are the int array. The line of the code where a* = array1 gets the address of the very first component of the array in the variable a.
After using header files, we define a structure with the variable of “a”. In this structure, we declare two static functions and pass the argument of the pointer as well as a variable that is the size of an array. The code’s single object is built at runtime in C++ using the new operator. In the function, we display our static arrays. In the main body, we take a pointer object and two arrays with the name of array1 and array2 respectively having different sizes. After that, we utilize the delete[] operator to delete the array.
Here, we get the first array with help of the object and delete it and then the second array again using the object and delete it.
Conclusion
This article described the array and the deletion of the arrays in the C++ language. We also discussed why we need to delete the array with the help of several approaches that are used for deleting the array in C++. We delete the array having no value and also add elements in the array then delete it last by using a pointer. By this article, we understand that we can also delete two or more arrays at the same time with the help of objects.