?
Solved

How do you delete a multi-dimensional array?

Posted on 2004-08-27
7
Medium Priority
?
267 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 80 total points
ID: 11917975
Example:

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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