How do you delete a multi-dimensional array?

Posted on 2004-08-27
Last Modified: 2010-04-01

I know to delete a normal array in a destructor is just

delete [] foo;

how do you delete a multi-dimensional array?

Question by:CBMLude
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 30

Expert Comment

ID: 11917957
>>how do you delete a multi-dimensional array?

You first need to iterate through each item in the array, and delete it.

Author Comment

ID: 11917974
ohhh.. okie so like this?

char board[4][4];

for (int i = 0; i < 4; i++)    
    delete [] board[i];

    delete [] board;
LVL 30

Accepted Solution

Axter earned 20 total points
ID: 11917975

      for (int i = 0;i < size_of_foo;++i)
            delete [] foo[i];
      delete [] foo;
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11917984
heh.. just beat you to it... thanks!
LVL 30

Expert Comment

ID: 11917991
>>ohhh.. okie so like this?
>>char board[4][4];

That is not a dynamic array.  That is a static variable, and you don't delete that type of variable.

LVL 30

Expert Comment

ID: 11918005
int** array2d;
int     n = 100;
int     m = 50;

array2d = new int*[n]; // allocating memory for array of pointers
for(int i = 0; i < n;i++)
   array2d[i] = new int[m];

for(int k = 0; k < n;k++)
    for(int j = 0; j < m;j++) array2d[k][j] = 0;

//free memory
for(i = 0; i < n;i++) delete[] array2d[i]; //free memory for each                                                           //pointer
delete[] array2d;    //delete array of pointers

Author Comment

ID: 11929913
okie say I have a multidimensional array in my private section of my class.  And I just created a new object

foo *a = new foo();

is my destructor this?

  for (int k=0; k < rowSize; k++)
    delete [] bar[k];
  delete [] bar

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 89
Getting IP address 8 121
Embarcadero C++ Builder XE2 TDateTime 8 90
Compile GLUT with Visual Studio 2015 1 229
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question