By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,124 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,124 IT Pros & Developers. It's quick & easy.

dynamic multidimensional array question

P: 53
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
Share this Question
Share on Google+
6 Replies


P: 53
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

P: 53
also do you delete the 2d array like you would with a normal dynamic array

delete [] 2d array
Apr 4 '08 #3

P: 5
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

P: 5
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
Expert Mod 5K+
P: 9,197
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

P: 53
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.