Solved

How do you delete a multi-dimensional array?

Posted on 2004-08-27
7
256 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

773 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