I posted this message earlier in the C forum, and was told this is a C++ problem so I am posting it here. I hope I am not offending anybody with a cross-post.
Following some literature I was able write the code below to dynamically allocate a two dimensional array. I made the attempt to extrapolate the code for a three dimensional array. The code below for the 3-D array compiles, but gives me a fatal run-time error. Since the 3-D array is not used anywhere in the code (I am just setting it up for now), the run-time error must come from the algorithm I am using to allocate. Evidently, I don't fully understand what is going on. I appreciate any help I can get.
Thanks, Jeff
Code:/*******TWO DIMENSIONAL ARRAY*******/ int nx,ny; struct GRID { float x1; float y1; float x2; float y2; }; GRID **grid2, *grid1; nx = 8; ny = 4; grid1 = new GRID[nx*ny]; grid2 = new GRID * [nx]; for (i=0; i<nx; i++) grid2[i] = &(grid1[i*ny]); // Sets up a 2-D array with dimension grid[8][4] /*******THREE DIMENSIONAL ARRAY*******/ int ndt,nx,ny; struct CELL { float height; int col; }; CELL ***cell3,**cell2, *cell1; ndt = 100; nx = 8; ny = 4; cell1 = new CELL[ndt*nx*ny]; cell2 = new CELL * [ndt*nx]; cell3 = new CELL ** [ndt]; for (t=0; t<ndt; t++) cell2[t] = &(cell1[(t*nx)]); for (t=0; t<ndt; t++) for (i=0; i<nx; i++) cell3[t][i] = &(cell2[(t*nx)][(i*ny)]); // Sets up a 3-D array with dimension cell[100][8][4]



LinkBack URL
About LinkBacks


