This article is on how to delete an array in C . It also includes the deleting the pointer array. Before that, it has to be stated that there are two principal ways of creating an array in C , which depends on the kind of memory used. When a program is running, it is given two memory portions: normal memory, which it uses; and free store, which it may or may not use. An array can be created in normal memory, or in free store. The syntaxes to create an array in either memory portion are different; otherwise, it is the same array. The ways in which they can be deleted are also different.

Ordinary Array

An array can be created in normal memory as follows:

char arr[] = {‘P’, ‘Q’, ‘R’, ‘S’, ‘T’};

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:

char *ptr = new char[5] {‘P’, ‘Q’, ‘R’, ‘S’, ‘T’};

The same array can be created in the C main function as follows:

char *ptr = new char[5];

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 Ordinary array

– Deleting Dynamically created Pointer Array of Free Store

– Conclusion

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:

#include

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().

    #include


    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:

#include


    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:

#include

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 library can still be included as in the following program for ordinary array:

    #include


    #include


    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 library can still be included, as in the following program, for dynamic pointer array in free store:

    #include


    #include


    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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Chrysanthus-150×150.jpeg6265c66d6865b.jpg" height="112" src="data:image/svg xml,” width=”112″>

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.