C++ array reference

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 4
Reputation: casperguru is an unknown quantity at this point 
Solved Threads: 0
casperguru casperguru is offline Offline
Newbie Poster

C++ array reference

 
0
  #1
Feb 23rd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: C++ array reference

 
0
  #2
Feb 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: casperguru is an unknown quantity at this point 
Solved Threads: 0
casperguru casperguru is offline Offline
Newbie Poster

Re: C++ array reference

 
0
  #3
Feb 23rd, 2008
Originally Posted by vijayan121 View Post
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: C++NOOOB is an unknown quantity at this point 
Solved Threads: 0
C++NOOOB C++NOOOB is offline Offline
Newbie Poster

Re: C++ array reference

 
0
  #4
Sep 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 308
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: C++ array reference

 
0
  #5
Sep 25th, 2009
Originally Posted by C++NOOOB View Post
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:
  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:
  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.
  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.
  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
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,175
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster

Re: C++ array reference

 
0
  #6
Sep 25th, 2009
I would recommend to use
  1. void foo(data_type * Array, int Size),
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: C++NOOOB is an unknown quantity at this point 
Solved Threads: 0
C++NOOOB C++NOOOB is offline Offline
Newbie Poster

Re: C++ array reference

 
0
  #7
Sep 28th, 2009
Thanks JasonHippy, firstPerson. I really appreciate you taking the time for explaining.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC