C++

Delete 2d Array C++

A two-dimensional array could be constructed in two distinct ways: with regular memory or free storage. If a program is executing, it has access to both its usual memory and additional memory. The code is not compelled to be using the free storage, which is additional Memory. In regular memory, the program will generate a standard two-dimensional array. If the program intends to construct the same 2-dimensional array in the free storage, it can do it dynamically.

In each type of memory, the method for defining a two-dimensional array varies. Simply let a normal two-dimensional array go out of range to delete it. Utilize the delete[] operator effectively to delete a two-dimensional array generated in the free library. Let’s discuss how to delete a two-dimensional array in C++:

Delete a Two-Dimensional Simple Array

For a two-dimensional normal array to go out of range needs to delete. By a nested domain, the program indicates this:

#include <iostream>

using namespace std;

    int main()
    {
        if (1 == 1) {
            string arr2D[][4] = {{"ZZ", "ZY", "ZX", "ZW"},
                                {"MM", "ML", "MK", "MJ"},
                                {"II", "IH", "IG", "IF"},
                                {"PP", "PO", "PN", "PM"}};
cout<< arr2D[1][1] <<endl;
        }

        return 0;
    }

First, we have to introduce the header file <iostream>. We declare a two-dimensional array and specify the elements of this array. The ‘cout’ statement is applied to the first row and first column. So we get the element present on the first row and first column. Other than that, all elements are deleted. We terminate the program by entering the ‘return 0’ command.

‘ML’ is the outcome after running the above code. The nested context is a section in the if-construct. At the end of the section, the array is no longer valid. There has been a remark indication beneath the function portion in the code. The program would not compile if it has been eliminated, and an error notification may appear.

The standard two-dimensional array specified in the body of the function terminates after the function call in the following code:

#include <iostream>

using namespace std;
    void fn()
    {
        string arr2D[][4] =   {{"ZZ", "ZY", "ZX", "ZW"},
                               {"PP", "PO", "PN", "PM"},
                               {"II", "IH", "IG", "IF"},
                               {"MM", "ML", "MK", "MJ"}};

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

    int main()
    {
fn();

        return 0;
    }

At the start of the program, we include the header file <iostream>. The standard namespace is being utilized. We call the void fn(). Here we define the elements of the two-dimensional array. We utilize the ‘cout’ statement to get the required element of the defined array.

After all this, we apply the main() function. Inside the body of this function, we declare fn(). At the end ‘return 0’ command is being used.

At the global level, assigning a value to a two-dimensional component before declaration has not been permitted.

Delete a Dynamically Constructed Two-Dimensional Free Storage Pointer Array

At the larger level, allocation before declaration would not be required. It is useful to have a two-dimensional array definition in a nested domain in the C++ primary function for pedagogical purposes. The expression “delete[] 2-dimensional array” is being used to delete a 2-D array that was specified in the program. This deletion must occur within its domain to relieve memory and reduce memory overflow. By a nested scope, the subsequent program exemplifies this:

#include <iostream>

using namespace std;
int main()

    {
        if (1 == 1) {
            string (*ptr2D)[5] = new string[5][5] {{"ZZ", "ZY", "ZX", "ZW"},
                               {"PP", "PO", "PN", "PM"},
                               {"II", "IH", "IG", "IF"},
                               {"MM", "ML", "MK", "MJ"},
                               {"EA", "EB", "EC", "ED", "EE"}};
cout<< ptr2D[0][0] <<endl;

                delete [] ptr2D;

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

        return 0;
    }

After including the library <iostream> the standard namespace is used. The main() function is now declared. If the condition is accomplished. The 2D array’s pointer is constructed. After that, we define a two-dimensional array and provide its elements. On zero rows and zero columns, the ‘cout’ statement is used. The delete[] technique is being applied. We have used the ‘cout’ statement once more after deleting it. We exit the program by executing the command ‘return 0’.

After deleting the elements of the array, the code reverts to nothing.

Pointer-to-Pointer Two-Dimensional Free Memory Array

As a pointer-to-pointer array, a two-dimensional array could be generated. In this situation, every one of the rows must be eliminated first, followed by the remaining 1d array. In the C++, the succeeding instance demonstrates this:

#include <iostream>

using namespace std;
int main()

    {
        string **ptr2D = new string*[3];  
        ptr2D[0] = new string[5];
        ptr2D[0][0] = "ZZ"; ptr2D[0][1] = "ZY"; ptr2D[0][2] = "ZX"; ptr2D[0][3] = "ZW";
        ptr2D[1] = new string[5];
        ptr2D[1][0] = "MM"; ptr2D[1][1] = "ML"; ptr2D[1][2] = "MK"; ptr2D[1][3] = "MJ";
        ptr2D[2] = new string[5];
        ptr2D[2][0] = "II"; ptr2D[2][1] = "IH"; ptr2D[2][2] = "IG"; ptr2D[2][3] = "IF";

cout<< ptr2D[2][2] <<endl;

for(int i = 0; i< 3; ++i) {
delete[] ptr2D[i];  
        }
delete[] ptr2D;
cout<< ptr2D[1][1] <<endl;
        return 0;
    }

The header file <iostream> is imported at the beginning of the program. The default namespace has been used. The string pointer to pointer is first constructed inside the main() function’s body. The components of the two-dimensional array have then been defined. The elements are specified by their rows and columns. To get the item on the second row and second column of the array, we use the ‘cout’ statement.

We’ve been employing the ‘for’ loop to first delete the rows of the array. The loop variable is initialized, and then a condition is applied to it. Lastly, we have incremented the value of the loop variable. We employ the delete[] method to eliminate additional entities. The command ‘return 0’ is used to end the code.

To eliminate the two-dimensional array in the free library, first, remove all of the rows using the delete[] method, then delete the main 1-d pointer array.

Conclusion

In this article, we have described different methodologies for deleting the 2D array in C++. Simply let a two-dimensional normal array go out of range to delete it. If the two-dimensional array were in free storage, it would be eliminated using the delete[] operator to release memory in the domain where it is specified. If the two-dimensional array in the free library has been constructed using standard notation, then the deletion will be as simple as “delete[] two-dimensional ArrayName.”

If it had been constructed as a pointer-to-pointer, remove the rows first by using “delete[] two-dimensional ArrayName[i]” and then delete the existing 1-dimensional array by using “delete[] two-dimensional ArrayName”.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content