| | |
C++ array reference
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
void foo(int* a) and void foo(int a[]) are identical; they are not distinguishable from each other. they are the samevoid 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.
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
void foo(int* a)andvoid 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.
•
•
•
•
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.
When passing an array via a reference:
C++ Syntax (Toggle Plain Text)
void foo(int (&A)[10])
But if you pass the array by value:
C++ Syntax (Toggle Plain Text)
void foo(int A[]);
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)
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.
C++ Syntax (Toggle Plain Text)
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
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Those who understand binary .....
And those who don't!
I would recommend to use
C++ Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- passing by reference a 2D array in C++ (C++)
- Creating dynamic array structures (C++)
- need help understandin how to pass by reference. (Java)
- passing arrays,reference and value parameters (C)
- Referencing a single dimension of a multi-dimensional array (VB.NET)
- How do I create a program using an Array ? (C++)
- Accessing functions from base class (C)
- Array limit (C)
Other Threads in the C++ Forum
- Previous Thread: My C++ code is resulting in an output of hexadecimal notation. Not Good!
- Next Thread: printing vectors... new at C++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






