943,514 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17547
  • C++ RSS
Feb 23rd, 2008
0

C++ array reference

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
casperguru is offline Offline
4 posts
since Feb 2008
Feb 23rd, 2008
0

Re: C++ array reference

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.
Last edited by vijayan121; Feb 23rd, 2008 at 2:13 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Feb 23rd, 2008
0

Re: C++ array reference

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
casperguru is offline Offline
4 posts
since Feb 2008
Sep 25th, 2009
0

Re: C++ array reference

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++NOOOB is offline Offline
12 posts
since Sep 2009
Sep 25th, 2009
0

Re: C++ array reference

Click to Expand / Collapse  Quote originally posted by C++NOOOB ...
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:
C++ Syntax (Toggle Plain Text)
  1. 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:
C++ Syntax (Toggle Plain Text)
  1. 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.
C++ Syntax (Toggle Plain Text)
  1. void foo(CLargeClass myLargeClass); // pass by value
  2. void foo(CLargeClass *myLargeClass); // pass by pointer
  3. 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.
C++ Syntax (Toggle Plain Text)
  1. void foo(const CLargeClass *myLargeClass) const;
  2. 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
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009
Sep 25th, 2009
0

Re: C++ array reference

I would recommend to use
C++ Syntax (Toggle Plain Text)
  1. void foo(data_type * Array, int Size),
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 28th, 2009
0

Re: C++ array reference

Thanks JasonHippy, firstPerson. I really appreciate you taking the time for explaining.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C++NOOOB is offline Offline
12 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: My C++ code is resulting in an output of hexadecimal notation. Not Good!
Next Thread in C++ Forum Timeline: printing vectors... new at C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC