A 2D array can be created in two ways: using the normal memory or using the free store. When a program is running, it has its normal portion of memory and extra memory to use. The program is not obliged to use the extra memory called free store. The program would create an ordinary 2d array in normal memory. If the program is to create the same 2d array in the free store, then it would have to do so dynamically. The syntaxes to create the two-dimensional array in either kind of memory, are different. To have delete an ordinary 2d array, just let it go out of scope. To delete a 2D array, created in free store, use the delete[] operator appropriately.

Creating Ordinary 2D Array

The following statement, creates an ordinary 2d array:

string arr2D[][5] = {{“AA”, “AB”, “AC”, “AD”, “AE”},

{“BA”, “BB”, “BC”, “BD”, “BE”},

{“CA”, “CB”, “CC”, “CD”, “CE”},

{“DA”, “DB”, “DC”, “DD”, “DE”},

{“EA”, “EB”, “EC”, “ED”, “EE”}};

If this array is created in the global scope, it cannot be used (e.g. reassigned an element value) in the global scope. However, it can have any of its elements reassigned a value in the other scopes.

To have delete this array, just let it go out of scope. If it was created in a scope other than the global scope, it would go out of scope at the end of its block (}). If it was created in the global scope, it would only go out of scope at the end of the program.

Free Store 2D Array

The following statement shows how the above array but with a different pointer name can be created dynamically in free store:

string (*ptr2D)[5] = new string[5][5] {{“AA”, “AB”, “AC”, “AD”, “AE”},

{“BA”, “BB”, “BC”, “BD”, “BE”},

{“CA”, “CB”, “CC”, “CD”, “CE”},

{“DA”, “DB”, “DC”, “DD”, “DE”},

{“EA”, “EB”, “EC”, “ED”, “EE”}};

Note how the 2D array has been created with the new operator. The name of the array, is ptr2D.

If this array is created in the global scope, it cannot be used (e.g. reassigned an element value) in the global scope. However, it can have any of its elements, reassigned a value in the other scopes.

To delete this array, use the delete[] operator, as shown below. The array in free store cannot really be deleted by letting it go out of scope. It must be deleted with the delete [] operator, in its scope, in order to free memory.

Article Content

– Introduction – see above

– Deleting a 2D ordinary Array

– Deleting Dynamically created 2D Pointer Array of Free Store

– Conclusion

Deleting a 2D Ordinary Array

A two-dimensional ordinary array is deleted by just letting it go out of scope. The following program illustrates this with a nested scope:

#include

using namespace std;

    int main()


    {


        if (1 == 1) {


            string arr2D[][5] = {{“AA”, “AB”, “AC”, “AD”, “AE”},


                                {“BA”, “BB”, “BC”, “BD”, “BE”},


                                {“CA”, “CB”, “CC”, “CD”, “CE”},


                                {“DA”, “DB”, “DC”, “DD”, “DE”},


                                {“EA”, “EB”, “EC”, “ED”, “EE”}};


cout<< arr2D[1][1] <<endl;


        }


        //cout<< arr2D[1][1] <<endl;

        return 0;


    }

The output is, BB. The if-construct has a block which is the nested scope. The array stops existing at the end of the block. There is a comment indicator, just below the block, in the program. If it is removed, the program will not compile and an error message will be issued. This results from the fact that the 2D array is dead at the end of the block.

In the following program, the ordinary 2D array declared in the function body, dies at the end of the function block:

#include

using namespace std;

    void fn() {


        string arr2D[][5] = {{“AA”, “AB”, “AC”, “AD”, “AE”},


                            {“BA”, “BB”, “BC”, “BD”, “BE”},


                            {“CA”, “CB”, “CC”, “CD”, “CE”},


                            {“DA”, “DB”, “DC”, “DD”, “DE”},


                            {“EA”, “EB”, “EC”, “ED”, “EE”}};


cout<< arr2D[1][1] <<endl;


    }


    //cout<< arr2D[1][1] <<endl;

    int main()


    {


fn();

        return 0;


    }

The output is still, BB. There is a comment indicator just below the function block in the program. If it is removed, the program will not compile and an error message will be issued. This results from the fact that the 2D array is dead at the end of the block. Also recall that, assignment of a value to a 2D element, after declaration, is not allowed in the global scope.

Deleting Dynamically created 2D Pointer Array of Free Store

Assignment after declaration is not allowed in the global scope. So, it is convenient to have a 2D array declaration, in a nested scope, in the C main function, for pedagogic reasons.

A two dimensional array, declared in the form above, is deleted with the syntax, “delete[] 2Darray”. This deletion has to take place in its scope to free memory and to avoid memory leakage. The following program illustrates this, with a nested scope:

    #include

    using namespace std;

    int main()


    {


        if (1 == 1) {


            string (*ptr2D)[5] = new string[5][5] {{“AA”, “AB”, “AC”, “AD”, “AE”},


                                {“BA”, “BB”, “BC”, “BD”, “BE”},


                                {“CA”, “CB”, “CC”, “CD”, “CE”},


                                {“DA”, “DB”, “DC”, “DD”, “DE”},


                                {“EA”, “EB”, “EC”, “ED”, “EE”}};


cout<< ptr2D[0][0] <<endl;

                delete [] ptr2D;

cout<< ptr2D[0][0] <<endl;


        }

        return 0;


    }

The output is, AA, from ptr2D[0][0]. After deletion, ptr2D[0][0] returns nothing. Though the other elements such as ptr2D[1][1] would still return a value, the array is considered deleted.

2D Free Store Array as Pointer-to-Pointer

A 2d array can be created as a pointer-to-pointer. In this case, all the rows will have to be deleted first before the one-dimensional array remaining, is deleted. The following program illustrates this in the C main function:

    #include

    using namespace std;

    int main()


    {


        string **ptr2D = new string*[3];  //no of rows


        ptr2D[0] = new string[5];


        ptr2D[0][0] = “AA”; ptr2D[0][1] = “AB”; ptr2D[0][2] = “AC”; ptr2D[0][3] = “AD”;


        ptr2D[1] = new string[5];


        ptr2D[1][0] = “BA”; ptr2D[1][1] = “BB”; ptr2D[1][2] = “BC”; ptr2D[1][3] = “BD”;


        ptr2D[2] = new string[5];


        ptr2D[2][0] = “CA”; ptr2D[2][1] = “CB”; ptr2D[2][2] = “CC”; ptr2D[2][3] = “CD”;

cout<< ptr2D[1][1] <<endl;

        //Free each sub-array (row)

for(int i = 0; i< 3; i) {


delete[] ptr2D[i];  


        }


delete[] ptr2D;  //Free the array of pointers

cout<< ptr2D[1][1] <<endl;

        return 0;


    }

The output is BB before deleting. After deleting, the return value from ptr2D[1][1] is nothing.

Now, this pointer 2D array in free store is a pointer one dimensional array, of pointer arrays. So, in order to delete the 2d array in free store, all the rows have to be deleted first with delete[] before the principal one-dimensional pointer array is deleted. This uses the delete[] operator scheme for a 2D array in free store.

Inclusion of Library

The new and delete operator, are actually defined in the library. However, including this library is optional.

Conclusion

To delete a 2D ordinary array, just let it go out of scope. If the 2D array is in free store, then it must be deleted with the delete[] operator to free memory in the scope in which it is declared. If the 2D array in free store was created with a conventional syntax, then a simple “delete [] 2DarrayName” would do for deletion. If it was created as a pointer-to-pointer, then the rows will have to be deleted first with “delete [] 2DarrayName[i]” and then the remaining 1D array (with no elements), be deleted with “delete [] 2DarrayName”.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Chrysanthus-150×150.jpeg6265c672a9971.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.