In this lecture, we will explore how to allocate arrays on the heap, manipulate the memory, and subsequently deallocate it using the ‘new’ and ‘delete’ operators in C++.
To allocate an array on the heap, we can use the new
operator conjunction with square brackets []
. For example, to allocate memory for an array containing five integers on the heap, we need to allocate 20 bytes (considering that each integer takes up 4 bytes). This allocation will look like this:
int* arr = new int[5];
This line of code allocates space on heap, for an array of five integers. A total of 20 bytes will be allocated from the heap.
Once we have our memory allocated for an array, we can access or change the content within it. For this purpose, the subscript operator (square brackets) is used in conjunction with the pointer, to access the memory. It looks something like this:
// Sets the first integer in the array to 33 arr[0] = 33; // Sets the third integer in the array to 55 arr[2] = 55;
In these line of code, the value 33 is assigned to the first position of our array, and the value 55 is assigned to the third position of our array.
After accessing and manipulating the allocated memory from heap, it’s equally important to free up the memory once we’re done with it. In our case, where an array was allocated on the heap, the simple delete
operator won’t suffice here. We need to use the delete[]
operator i.e. the delete
operator in conjunction with square brackets []
, like this:
delete[] arr;
This line of code will deallocate the memory of the array pointed to by the pointer arr
, but before that it will also call the destructir for each element of array.
If you try to delete an array allocated on heap using delete
operator instead of delete[]
operator, then it can cause undefined behaviour, like it might be possible that destructor of all the array elements will not be called or memory will not be complete deallocated.
therefore, it is important to remember that the delete
operator is used when a single object was allocated on the heap using the ‘new’ operator. However, if an array has been allocated on the heap using new[]
operator, then it needs to be deleted with the delete[]
operator.
To solidify the concept, let’s look at a practical example:
#include <iostream> int main() { // Allocating an array of 5 integers on the heap int *arr = new int[5]; // Setting the value of each variable in the array for (int i = 0; i < 5; i++) { arr[i] = i + 10; } // Iterating over the array and // printing them on the console for (int i = 0; i < 5; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; // Deleting the array allocated // on the heap delete[] arr; return 0; }
Output
10 11 12 13 14
In this code, we first allocated an array of 5 integers on the heap. We then set the value of each variable in the array. Subsequently, we iterated over the array of integers and printed the value of each integer in array. Finally, we deallocated the array using the delete[]
operator.
In this article, we learned how to allocate and deallocate an array on a heap using new[]
and delete[]
operators. Understanding and applying memory allocation and deletion effectively is essential to prevent common errors, improve efficiency, and promote cleaner, more maintainable code.