Archived

This topic is now archived and is closed to further replies.

Passing 2D Arrays Fast

This topic is 5700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


  
return_type function_name(struct_type **arrayname, otherargs,)
{
// code here

}
//or is using linear 2d arrays

return_type function_name(struct_type *arrayname, otherargs,)
{
// code here

}


you cant get faster then just passing the address for the array.

Share this post


Link to post
Share on other sites
Yeah, I guess that''d work the fastest, but my compiler gives me an error whenever I try to return a pointer to a 2d array. This is my function:

Tile** Map::GetTiles()
{
return TileMap;
}

TileMap is delcared as:

Tile TileMap[256][256];

However, I get an error saying "cannot convert Tile(*)[256] to Tile**

Do I have to return TileMap as a Tile*[256]?
Will that slow my program down any?

- Bong, James Bong

Share this post


Link to post
Share on other sites
Now the compiler gives me an error saying "cannot convert Tile* to Tile**"

I tried changing it to

return &(&(TileMap[0][0]));

but it gives me an error saying "not a 1value"

- Bong, James Bong

Share this post


Link to post
Share on other sites
The problem is that you''re trying to convert to an array of arrays to an array of pointers, which seems to work the same, but really isn''t. Anyway, you can pass it along as a linear array:


  
void fill128x256 (int *array)
{
for (int y = 0; y < 128; y++)
{
for (int x = 0; x < 256; x++)
{
array [y * 256 + x] = rand ();
}
}
}

void useless ()
{
int myarray [128][256];
fill128x256 (&(myarray [0][0]));
}


Also, take a look at this thread, in which I have also posted a class to encapsulate this behaviour.

Share this post


Link to post
Share on other sites