Sign in to follow this  

Multi-dimensional array as param

This topic is 3790 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

C++ What's the cleanest way to pass a multi-dimensional (2D) array of unknown size as a function parameter? An example function prototype and call would be greatly appreciated. And if you're quick to suggest using a vector instead, could you please drop an example of how you'd do that? Thank You. int thing[10][10]; void func(int array[?][?]) { // I want "thing" in here. }

Share this post


Link to post
Share on other sites
Thats the way to do it in theory.
My favorite way, in practice, is to store the multidimensional array in a struct, then pass the pointer to that struct. I actually used a class but structs work just as well.
I assume your using the buffer as a discrete data type. Thats what a typedef is designed to do.

Take for example a text buffer. I chose, for something i was doing once, to use a char * for each line in the text buffer. So, i had:

unsigned char data[10][10];

To pass it into a function was awkward. How do you avoid a memory access problem? You only passed the array, not any information about how large it was. In code you are limiting yourself to exactly 10x10 ints. Now try to go back and change it.

So I did this, (the real version was bigger but this is just an example)


typedef struct line_buffer
{
uint num_lines;
unsigned char ** data;
};

int initialize_buffer(unsigned int x, unsigned int y, line_buffer & B);


line_buffer B;
initialize_buffer(B);
renderer.display_text_buffer(&B);



and voila. You passed a multidimensional array, along with the information you needed to operate on it effectively. Now you can pass it to the renderer, without having to modify the renderer code when you change the size of your text buffer.

Share this post


Link to post
Share on other sites
The problem is that multi-dimensional arrays are a hack: because the storage is "rectangular", the compiler can translate indexing like [y][x] into a single index like [y * columns + x]. When you pass arrays of indeterminate size, though, they decay to pointers - and multi-dimensional arrays still decay to a *single*-level pointer (T[][] becomes T*, NOT T**), because there is only one actual layer of indirection.

EDIT: Actually, that's not quite accurate. AFAICT, this is.

There are ways to use templates (with non-type parameters), and reference-to-array types to make this work, but they're all more advanced than they really ought to be.

My suggestion is to look up boost::multi_array and use it.

Share this post


Link to post
Share on other sites

This topic is 3790 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this