Ordinary Array
An array can be created in normal memory as follows:
To delete this array, just let it go out of scope.
Free Store Array
An array can be created dynamically, during program execution, in free store. Such an array can be created as follows:
The same array can be created in the C++ main function as follows:
ptr[0] = 'P'; ptr[1] = 'Q'; ptr[2] = 'R'; ptr[3] = 'S'; ptr[4] = 'T';
Note of the use of the operator, new, here. The ptr is a pointer. arr above is also a pointer but was approached from a different angle. To delete this array, use the operator, delete[] as shown below. The array in free store cannot be deleted by letting it go out of scope. So, it must be deleted with the delete[] operator.
This article illustrates the different ways of deleting an array created in the two ways.
Article Content
– Deleting Dynamically created Pointer Array of Free Store
Deleting Ordinary Array
To delete an ordinary array, just let it go out of scope. Though the main() function is the capital function of a C++ program, it is still a function. In the following program, an array is created in a nested local scope (block) in the C++ main function:
using namespace std;
int main()
{
if (1 == 1) {
char arr[] = {'P', 'Q', 'R', 'S', 'T'};
cout<<arr[1] <<endl;
}
//cout<<arr[1] <<endl;
return 0;
}
The output is, Q. The nested scope is the block of the if-construct. The array was created in this block and used in the same block by printing the second value of the array. At the end of the block, the array variable dies. If the comment indicator just below the block is removed, the program will not be compiled, an error message will be issued. This will be because the array died at the end of the block.
The following program, illustrates a similar thing, but in a function, called fn().
using namespace std;
void fn() {
char arr[] = {'P', 'Q', 'R', 'S', 'T'};
cout<<arr[1] <<endl;
}
//arr[1] = 'U';
int main()
{
fn();
return 0;
}
The output is still, Q. If the comment indicator just below the function block is removed, the program will not compile and an error message will be issued. This will be because the array died at the end of the function block (scope). Also, note that assignment after declaration, is not allowed in the global scope.
Deleting Dynamically created Pointer Array of Free Store
Since assignment after declaration is not allowed in the global scope, the array in the free store will be done in a nested scope to the C++ main function block which is illustrated below. The delete[] operator is used in the nested scope, to delete the array, as follows:
using namespace std;
int main()
{
if (1 == 1) {
char *ptr = new char[5] {'P', 'Q', 'R', 'S', 'T'};
cout<<ptr[1] <<endl;
delete [] ptr;
cout<<ptr[1] <<endl;
}
return 0;
}
The output is one ‘Q’, from the first cout statement. Note that the name of the array, ptr, as argument (parameter) of the delete operator. The array,ptr for the free store, is declared, used, and deleted with the “delete []()” operator in the same nested scope. If it is let to go out of scope, without “delete [] ptr”, it would not really be deleted because it is in free store. Such an array must be deleted in its scope, after use with the delete[] operator. Deleting with the delete[] operator has to be done for the dynamic array in order to free memory (avoid memory leak).
The following program, illustrates a similar thing, but in the C++ main function scope:
using namespace std;
int main()
{
char *ptr = new char[5] {'P', 'Q', 'R', 'S', 'T'};
cout<<ptr[1] <<endl;
delete [] ptr;
return 0;
}
The output is still, Q as it should, with the index. All the code of interest is directly in the C++ main function. Though the main() function is the capital function of a C++ program, it is still a function of the function scope or function block.
Inclusion of Library Header
As noted above, no library has been included for the use of the new or delete operators. However, the new and delete operators are in the new library, which is implicitly included. The new and delete operators are of the global scope. The <new> library can still be included as in the following program for ordinary array:
#include <new>
using namespace std;
int main()
{
if (1 == 1) {
char arr[] = {'P', 'Q', 'R', 'S', 'T'};
cout<<arr[1] <<endl;
}
//cout<<arr[1] <<endl;
return 0;
}
The program works without any problem. The <new> library can still be included, as in the following program, for dynamic pointer array in free store:
#include <new>
using namespace std;
int main()
{
if (1 == 1) {
char *ptr = new char[5] {'P', 'Q', 'R', 'S', 'T'};
cout<<ptr[1] <<endl;
delete [] ptr;
}
return 0;
}
The program works without any problem. Note: including the new header (library) is not obligatory.
Conclusion
To delete an ordinary array, just let it go out of scope. To delete a dynamic pointer array of free store, use the operator, delete [] arrayName, in the same scope. The array delete operator, can have a second and/or third argument. However, that is discussion for some other time.