开发者

problem in dynamic allocation of 3-dimensional array

开发者 https://www.devze.com 2023-02-25 00:10 出处:网络
array_2D = new ushort * [nx]; // Allocate each member of the \"main\" array // for (ii = 0; ii < nx; ii++)
array_2D = new ushort * [nx];

// Allocate each member of the "main" array
//
for (ii = 0; ii < nx; ii++)
    array_2D[ii] = new ushort[ny];

// Allocate "main"开发者_Python百科 array
array_3D = new ushort ** [numexp];

// Allocate each member of the "main" array
   for(kk=0;kk<numexp;kk++)
       array_3D[kk]= new ushort * [nx];
   for(kk=0;kk<numexp;kk++)
       for(ii=0;ii<nx;ii++)
           array_3D[kk][ii]= new ushort[ny];

the values of numexp,nx and ny is obtained by user..

Is this the correct form for dynamic allocation for a 3d array....We know that the code is working for the 2D array...If this is not correct can anyone suggest a better method?


I think the simplest way to allocate and deal with a multidimensional array is to use one big 1d array (or better yet a std::vector) and provide an interface to index into correctly.

This is easiest to think about first in 2 dimensions. Consider a 2D array with "x" and "y" axis

    x=0   1   2
 y=0  a   b   c
   1  d   e   f 
   2  g   h   i

We can represent this using a 1-d array, rearranged as follows:

    y= 0 0 0 1 1 1 2 2 2
    x= 0 1 2 0 1 2 0 1 2
array: a b c d e f g h i

So our 2d array is simply

   unsigned int maxX = 0;
   unsigned int maxY = 0;
   std::cout << "Enter x and y dimensions":
   std::cin << maxX << maxY

   int array = new int[maxX*maxY];

   // write to the location where x = 1, y = 2
   int x = 1;
   int y = 2;
   array[y*maxX/*jump to correct row*/+x/*shift into correct column*/] = 0;

The most important thing is to wrap up the accessing into a neat interface so you only have to figure this out once

(In a similar way we can work with 3-d arrays

   z = 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
   y = 0 0 0 1 1 1 2 2 2 0 0 0 1 1 1 0 0 0 1 1 1 2 2 2
   x = 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2
array: a b c d e f g h i j k l m n o p q r s t u v w x

Once you figure out how to index into the array correctly and put this code in a common place, you don't have to deal with the nastiness of pointers to arrays of pointers to arrays of pointers. You'll only have to do one delete [] at the end.


Looks fine too me, so long an array of arr[numexp][nx][ny] is what you wanted.
A little tip: you can put the allocation of the third dimension into the loop of the second dimension, aka you allocate each 3rd dimension while the parent subarray gets allocated:

ushort*** array_3D = new ushort**[nx];
for(int i=0; i<nx; ++i){
  array_3D[i] = new ushort*[ny];
  for(int j=0; j<ny; ++j)
    array_3D[i][j] = new ushort[nz];
}

And of course, the general hint: Do that with std::vectors to not have to deal with that nasty (de)allocation stuff. :)

#include <vector>

int main(){
  using namespace std;
  typedef unsigned short ushort;
  typedef vector<ushort> usvec;
  vector<vector<usvec> > my3DVector(numexp, vector<usvec>(nx, vector<ushort>(ny)));
//           size of -- dimension 1 ^^^^^^ -- dimension 2 ^^ --- dimension 3 ^^
}
0

精彩评论

暂无评论...
验证码 换一张
取 消