Solved

How do you delete a multi-dimensional array?

Posted on 2004-08-27
7
257 Views
Last Modified: 2010-04-01
Hi,

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

delete [] foo;

how do you delete a multi-dimensional array?

Thanks!
0
Comment
Question by:CBMLude
[X]
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
7 Comments
 
LVL 30

Expert Comment

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

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

Author Comment

by:CBMLude
ID: 11917974
ohhh.. okie so like this?

char board[4][4];


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

    delete [] board;
0
 
LVL 30

Accepted Solution

by:
Axter earned 20 total points
ID: 11917975
Example:

      for (int i = 0;i < size_of_foo;++i)
            delete [] foo[i];
      delete [] foo;
0
Technology Partners: 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

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

Expert Comment

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

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

0
 
LVL 30

Expert Comment

by:Axter
ID: 11918005
Example:
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
0
 

Author Comment

by:CBMLude
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?

foo::~foo()
{
  for (int k=0; k < rowSize; k++)
  {
    delete [] bar[k];
  }
  delete [] bar
}
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 95
I could not build boost code, 10 106
Arduino EDI - Programming language 3 114
DCT of  2D array using fftw in c++ 9 90
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

756 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