Run-time error with dynamically allocated 3-D array

This is a discussion on Run-time error with dynamically allocated 3-D array within the C Programming forums, part of the General Programming Boards category; Following some literature I was able write the code below to dynamically allocate a two dimensional array. I made the ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Run-time error with dynamically allocated 3-D array

    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 to a the 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]

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code is C++ not C. C doesn't have the new operator.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Going to post in C++

    I guess you are right. Well then at the risk of cross-posting, I will take this up with the C++ forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 09:40 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. passing a 2dim dynamically allocated array to a funct
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2004, 06:55 PM

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