passing a reference to a multi-dimension array
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.
You *are* passing by reference.
Remember:
void my_func( float x[1000][1000][100]) {}
Remember:
void f( int *x )// is identical tovoid f( int x[] )
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?
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?
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement