Sign in to follow this  

passing a reference to a multi-dimension array

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

I have an array of floats n[1000][1000][100] and I want to pass a reference to it to a function (not a copy, due to its size). Usually when I define a function to work with pass-by-reference, I do this void my_func(float &n) { ... } I don't know how this works for an array. I think this will end up passing a copy void my_func(float &n[1000][1000][100]) { ... } which is not what I want. void my_func(float ***n) // which I think represents my array correctly {...} results in an invalid parameter conversion. I have worked with pointers before, but I admit I feel I'm lacking in some fundamental understanding of them and what's going on here. Any help would be very much appreciated. EDIT: Incase anyone wonders, I intend this structure to be a 3-dimensional matrix.

Share this post


Link to post
Share on other sites
You *are* passing by reference.

void my_func( float x[1000][1000][100]) {}




Remember:

void f( int *x )

// is identical to

void f( int x[] )


Share this post


Link to post
Share on other sites
Thank you =)

I was under the (clearly incorrect) presumption that specifying those dimensions was setting it up to allocate a copy.

EDIT:

Does this mean I can't pass arrays of different sizes to this function? Do I just have to make those numbers "big enough" to "buffer" the largest, so that smaller ones can fit? Or do the dimensions have to be exact in the function declaration?

Share this post


Link to post
Share on other sites
Quote:
Does this mean I can't pass arrays of different sizes to this function? Do I just have to make those numbers "big enough" to "buffer" the largest, so that smaller ones can fit? Or do the dimensions have to be exact in the function declaration?


Yes, because they are allocated as contigous block of memory. Dimensions aren't passed, just offest calculation is inserted into function. You could pass anything, but it would still calculate all dimensions as those defined in function.

Arrays have no dimensions. This is the cause of all buffer overflows.

std::vector, struct containing sizes, or explicitly specified dimensions are your options.

If this is C++, then arrays don't really have much place in this way. If C, then you're very much advised to specify bounds explicitly - nothing will stop you from writing outside of allocated memory.

Or better yet:
void f( float x[][1000][100] )


This is what you can do. First dimension can be unspecified. But all others need to be, so that function knows how to calculate the offsets.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Or better yet:
void f( float x[][1000][100] )


This is what you can do. First dimension can be unspecified. But all others need to be, so that function knows how to calculate the offsets.


Well, you can use templates as well:

template< size_t D1, size_t D2, size_t D3 >
void f( float ( &x )[D1][D2][D3] )


Still, I recommend std::vector, or perhaps boost::multi_array.

edit: and by the way, the above notation is for an actual reference to an array, not pointer degradation.


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Arrays have no dimensions. This is the cause of all buffer overflows.


This is not quite true. You will find that sizeof( x ) / sizeof( x[0] ) will give you the dimension of your array (assuming x is not a decayed pointer). An array is not the same thing as a pointer to the first element.


jfl.

Share this post


Link to post
Share on other sites

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