What are the difference in the passing mechanism of the following arrays

void foo(int* a)

void foo(int(&a) [10])

void foo(int a[])

and if all the three absolutely does the same thing then what is the point of keeping 3 of them and does any method have any performance gain as compared to other?

void foo(int* a) and void foo(int a[]) are identical; they are not distinguishable from each other. they are the same void foo(int(&a) [N]) a is a reference to an array of exactly N elements. disadvantage: N must be a constant known at compile time. advantage: size of the array is known in the called function. there can be no programming errors wrt size.

> does any method have any performance gain as compared to other?
no.

void foo(int* a) and void foo(int a[]) are identical; they are not distinguishable from each other. they are the same void foo(int(&a) [N]) a is a reference to an array of exactly N elements. disadvantage: N must be a constant known at compile time. advantage: size of the array is known in the called function. there can be no programming errors wrt size.

> does any method have any performance gain as compared to other?
no.

Thanks vijayan - much appreciated

So what's the difference between:

void foo(int(&A) [])

void foo(int A[])

If the contents of array A need to be modified by the function foo, which of the two is the correct usage?

Thanks.

So what's the difference between:

void foo(int(&A) [])

void foo(int A[])

If the contents of array A need to be modified by the function foo, which of the two is the correct usage?

Thanks.

As far as I understand it, they are both valid and correct.
When passing an array via a reference:

void foo(int (&A)[10])

The only prerequesite is you need to know the dimensions of the array upfront.

But if you pass the array by value:

void foo(int A[]);

Because it's an array you're passing in, Its basically the same as passing a pointer. An array object contains the memory address of (and therefore a pointer to) the first position in the array.
This way you don't need to know the size of the array upfront. (That's not to say that the size of the array isn't important...Don't go indexing out of bounds or anything. You just don't need to specify the size of the array in the function prototype!)

So which you choose really depends what you're trying to do I guess!

Whereas if you were trying to pass a large class object to a function you could
pass in the following ways:
e.g.

void foo(CLargeClass myLargeClass); // pass by value
void foo(CLargeClass *myLargeClass); // pass by pointer
void foo(CLargeClass &myLargeClass); // pass by reference

Passing by value in this case would incur a performance hit because normally; passing by value creates a copy of the object being passed. (except in the case of arrays, which we've already talked about..Arrays passed by value are basically treated as pointers).

The other thing about passing by value is: any changes made to the passed in object cannot be seen outside of the function. (Again, except in the case of arrays!)

Whereas if you pass a pointer or reference into the function, then no copy of the object is made inside the function..The other advantage is that changes made to the object inside the function, can be seen outside of it.

If you wanted to ensure that your function doesn't alter anything in your object and you don't want to incur a performance hit by passing by value, then you can use a const pointer or const reference as a parameter. You can also make the function const too.
e.g.

void foo(const CLargeClass *myLargeClass) const;
void foo(const CLargeClass &myLargeClass) const;

Now the functions cannot make any changes to the passed in object....Unless any of the class members have been declared mutable...A specialisation of const which is kinda like a const that isn't particularly const! ;)

Information is correct to the best of my knowledge, any technical errata on my part will no doubt be corrected rather quickly (and probably forcefully!) by some of the slightly more experienced programmers here! heh heh! :)

But if there are any inaccuracies, I apologise upfront. That was just my understanding of the issue!

Cheers for now,
Jas

Thanks JasonHippy, firstPerson. I really appreciate you taking the time for explaining.

This article has been dead for over six months. Start a new discussion instead.