468,469 Members | 2,168 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,469 developers. It's quick & easy.

dynamic multidimensional array question

hi, im trying to create a multidimensional array and then pass along a few funcitons.

this is the way i have created, im sure its the correct way of doing it?

// TerrainMapData is struct

TerrainMapData **TerrainData ;

TerrainData = new TerrainMapData *[TerrainWidth];

for( USHORT usLoop = 0; usLoop < TerrainLength; usLoop++ )
{
TerrainData[ usLoop ] = new TerrainMapData[ TerrainWidth ];
}

now i dont know how to pass it correctly?
would you call funcitons that would have this:

void SmoothTerrain( TerrainMapData *&TerrainData );

or would it have to be SmoothTerrain( TerrainMapData **&TerrainData );

also what would you think would be more efficient and quicker to do this?

maybe using a vector list or do it some other way, instead of passing the whole multidimensional array?
Apr 4 '08 #1
6 1682
ok, SmoothTerrain( TerrainMapData **&TerrainData ); works, im sure that means passing the whole 2d array?

but i would very much appreciate if anyone knows a more efficient was of doing this.
Apr 4 '08 #2
also do you delete the 2d array like you would with a normal dynamic array

delete [] 2d array
Apr 4 '08 #3
ok, SmoothTerrain( TerrainMapData **&TerrainData ); works, im sure that means passing the whole 2d array?
No, you are passing a reference to your multi-dimensional array. I'm not sure there's any benefit in using a reference in this case, as you may as well just use SmoothTerrain( TerrainMapData **TerrainData);

but i would very much appreciate if anyone knows a more efficient was of doing this.
Using **& or simply ** is the most efficient way to pass this around (don't quote me on this, but I'm sure both methods would result in an argument of size sizeof(unsigned int) being passed to SmoothTerrain).
Apr 4 '08 #4
also do you delete the 2d array like you would with a normal dynamic array

delete [] 2d array
Essentially, yes. Your delete needs to mirror what your new did in the first place, so:

Expand|Select|Wrap|Line Numbers
  1. for( USHORT usLoop = 0; usLoop < TerrainLength; usLoop++ )
  2. {
  3.    delete [] TerrainData[ usLoop ];
  4. }
  5.  
  6. delete [] TerrainData;
  7.  
Apr 4 '08 #5
weaknessforcats
9,207 Expert Mod 8TB
You are not creating a 2D array. What you are doing is creating a 1D array of pointers and assigning the address of other 1D arrays o each pointer. That is not a 2D array.

Also,
TerrainMapData **TerrainData ;
Is a pointer to a TerrainData pointer. You cannot use this for the address of a 2D array.

Please read this article: http://bytes.com/forum/thread772412.html.
Apr 4 '08 #6
thanks everyone for there reply's
Apr 8 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Gregory L. Hansen | last post: by
9 posts views Thread by Charles Banas | last post: by
9 posts views Thread by JoeC | last post: by
24 posts views Thread by VijaKhara | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.