3D arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crispin
    New Member
    • Jan 2007
    • 11

    3D arrays

    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 = thisFirstElemen t; a < totalFirstEleme nts; 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
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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?

    Comment

    • ganymede
      New Member
      • Feb 2007
      • 2

      #3
      Hi there,

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

      Code:
      for ( iterate through 1st array dimension)
      {
        for ( iterate through 2nd array dimension)
        {
          for (iterate through 3rd array dimension)
           {
            delete array [1st] [2nd] [3rd]
           }
         }
      }
      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...

      Comment

      • crispin
        New Member
        • Jan 2007
        • 11

        #4
        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

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by crispin
          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?

          Comment

          • crispin
            New Member
            • Jan 2007
            • 11

            #6
            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

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              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 Files

              Comment

              • meatstu
                New Member
                • Feb 2007
                • 13

                #8
                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.

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #9
                  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)

                  Comment

                  • ganymede
                    New Member
                    • Feb 2007
                    • 2

                    #10
                    If you create the array within a function it will be deleted the moment the function ends

                    {
                    array[x][y][z];
                    blabla;
                    } // array deleted

                    Comment

                    Working...