Dynamic 3D array allocation

This is a discussion on Dynamic 3D array allocation within the C++ Programming forums, part of the General Programming Boards category; Using new & delete, how to I dynamically allocate & deallocate a 3-dimensional array? -Vulcan...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    22

    Dynamic 3D array allocation

    Using new & delete, how to I dynamically allocate & deallocate a 3-dimensional array?

    -Vulcan

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    If all the dimensions should be dynamical, you could do like this:

    char *dynarray;
    long dimx, dimy, dimz;

    //macro for calculating the position of a coordinate in the array:
    #define getarr(x,y,z) dynarray[x+y*dimx+z*dimx*dimy]

    //code for declaring and using the array:

    dimx = 5; dimy = 5; dimz = 5;

    dynarray = new char[dimx*dimy*dimz];

    getarr(0,0,0) = 0;

    delete [] dynarray;
    // Gliptic

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    22

    yeah

    Thats how i did do it before, map a 3D array to a 1D space via the same way you showed. but what i wanted to do so if you had a 3D space
    [x][y][z]

    you could acess the array as such
    data[x][y][z]

    where data is a ***
    pointer to a pointer to a pointer.

    -Vulcan

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    You can't do it.
    // Gliptic

  5. #5
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    data[x][y][z]
    You can't do it.
    Wrong! There sure is possible solution. Look at this:
    Code:
    const int Xsize = 20;
    const int Ysize = 30;
    const int Zsize = 80;
    
    char ***3DArray;
    
    // Allocating array:
    
    3DArray = new char**[Xsize];
    
    for (int i=0; i<Xsize; ++i)
    {
      3DArray[i] = new char*[Ysize];
    
      for (int j=0; j<Ysize; ++j)  3DArray[i][j] = new char[Zsize];
    }
    
    // destroying array:
    
    for (int i=0; i<Xsize; ++i)
    {
      for (int j=0; j<Ysize; ++j)  delete [] 3DArray[i][j];
    
      delete [] 3DArray[i];
    }
    
    delete [] 3DArray;
    Now you can access this array with 3DArray[2][5][25]
    Making error is human, but for messing things thoroughly it takes a computer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  2. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 09:09 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  4. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 12:48 PM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 05:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21