By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

3D arrays

P: 11
Hi,

I have been working on allocating and deleting multi-dimensional arrays...
I gather that to delete a 2D array correctly, you need to do the following:

for (int a = thisFirstElement; a < totalFirstElements; a++)
{
delete[] blabla[a];
}

But how would I delete a 3D array? Would it just be delete[][] blabla[a] or am I missing something?

Thanks,
Crispin
Feb 22 '07 #1
Share this Question
Share on Google+
9 Replies


DeMan
100+
P: 1,806
Not quite familiar with the exact commands you are using, but I think it's helpful to think of multi dimensional arrays as arrays of arrays......

Thus a[] = array
a[][] = an array of arrays
a[][][] =an array of (array of arrays)
etc.

So an array operation should work on multi-dimensional arrays, irrespective of the dimension....

As always, I'm sure if I'm wrong, someone will correct me?
Feb 22 '07 #2

P: 2
Hi there,

I'm still a student but I think this is what you need.

Expand|Select|Wrap|Line Numbers
  1. for ( iterate through 1st array dimension)
  2. {
  3.   for ( iterate through 2nd array dimension)
  4.   {
  5.     for (iterate through 3rd array dimension)
  6.      {
  7.       delete array [1st] [2nd] [3rd]
  8.      }
  9.    }
  10. }
  11.  
Let's say all array's go from 0 to MAX.
then the first element to be deleted is
array[0][0][0]
second one
array[0][0][1]
3rd
array[0][0][2]
lets say the 3rd dimension reaches MAX value, the 2nd dimension will then be incremented(by 1) and repeating the 3rd dimesion loop.
so:
array[0][1][0] untill array[0][1][MAX]
then:
array[0][2][0] untill array[0][2][MAX]
when the 2nd dimension reaches it MAX( array[0][MAX][MAX])
we can include the 1st dimension in the acces loops, because the array[0][..][..] has now been completely deleted we goto:
array[1][...][...] basically the above will be repeated untill the 2nd and 3rd dimension reach their MAX value and the 1st dimension is incremented again to:
array[2][...][...] untill array[MAX][..][...] has been reached and all elements have been iterated through.

If you don't understand this, try drawing the situation, I suggest drawing it 2D because it's easy and the logic behind this is analog to n dimensions.
you iterate through the x and then through the y
delete all X within the y== 0 range
then increment y +=1, delete all X within the new y == 1 range
etc etc etc...
Feb 22 '07 #3

P: 11
Hi,

Thanks to both of you for your replies --

So is it necessary to create a loop and delete every element individually when the array is larger than 2D? I was king of hoping that I could just use
delete[][] blabla[firstDimension] for a 3D array, but I don't wan't any memory leaks.

Cheers,
Crispin
Feb 22 '07 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Hi,

Thanks to both of you for your replies --

So is it necessary to create a loop and delete every element individually when the array is larger than 2D? I was king of hoping that I could just use
delete[][] blabla[firstDimension] for a 3D array, but I don't wan't any memory leaks.

Cheers,
Crispin
Are you trying to delete the whole array, or just one element of the array?

Is this something that might be better placed in another data structure or even its own class?
Feb 22 '07 #5

P: 11
The whole array so that it can be reallocated --

Ganymede -- re. the PM. Sorry, I didn't mean to ignore your post - I was just hoping that there was a way to delete the whole array without cycling through every single element.

Cheers,
Crispin
Feb 22 '07 #6

RedSon
Expert 5K+
P: 5,000
Its helpful to think of 3-D arrays in terms of x, y and z coordinates. So if you want to look at all the elements that are on the x origin you would iterate through each value of y and z from 0 to MAX.

You can have 4, 5, 6 or even 100 dimensional arrays. You can think of it like this...

Ok check out the attachment. I know it looks like a graph but its actually a 3D array of size [3][3][3] the last nodes are where your data is, the green node is accessed by array[2][2][0]. Maybe now that I think about it, this might be doing more harm then good, since it is confusing the whole tree vs. array thing. This only works if you don't have trouble thinking about array indexes as pointers to other data.
Attached Images
File Type: bmp 3darrays.bmp (21.4 KB, 255 views)
Feb 22 '07 #7

P: 13
I am a student myself and the answer ganymede has posted is the way I have always done any initialising or deleting of arrays.
I dont know of any other way.
Stu.
Feb 22 '07 #8

DeMan
100+
P: 1,806
It all depends on what you mean by delete..., and what the arrays contain.

A three dimensional array of a basic type is a contiguous block of memory.....

if you have array[][][] whose sizes are 2, 3, 4 your total size is 24 and you could
memset(array, 0, 24);

By the way, as far as I know, arrays won't cause memory leaks unless you explicitly allocate memory for them. If you do, then you know how big they are and can memset them quite easily (and/or free them if the data is not sensitrive)
Feb 22 '07 #9

P: 2
If you create the array within a function it will be deleted the moment the function ends

{
array[x][y][z];
blabla;
} // array deleted
Feb 27 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.