![]() |
![]() |
![]() |
|
|
#1 (permalink) |
|
Junkie
Join Date: Nov 2003
|
[C++] passing pointers by reference?
Is it possible to pass a pointer in C++ by reference without just using a double pointer?
I have some code that should work but the refrence portion of it does not seem to be working. Here is a function similar to what i want to do without all the details. Code:
void foo(int *&a)
{
int* b=new int[10];
//loop some number of times
//Do some copy operation from b to a
swap(a,b);
delete[] b;
}
|
|
|
|
|
|
#3 (permalink) |
|
Rookie
Join Date: Jun 2005
|
thanks
That's different. I never actually thought of using a reference by pointer, but I suppose it does fit within the rules. I use the double pointer just so I know for sure that it's pointing to something else, which allows me to see immediately which variables are byref and which ones are not. When I start using references too much, I usually end up with Visual Basic like byref problems. Thanks for the tidbit though. The knowledge may come in handy sometime.
|
|
|
|
|
|
#4 (permalink) |
|
Rookie
Join Date: Jul 2003
|
If my teachings were correct, it doesn't make too much difference whether or not it's by reference, since a pointer is basically an int. I was taught that with smaller-sized data, passing by reference is actually a little bit slower because the compiler has to do some pointer math or keep an extra table or something. I'm fuzzy on the exact details, though.
|
|
|
|
|
|
#5 (permalink) |
|
It's Just A Game
Join Date: Sep 2003
Location: San Francisco
|
I think references are usually treated exactly the same as pointers in the compiled machine code, but I'm not a compiler expert. The compiler just handles the stuff that the programmer would need to handle with pointers like getting addresses and dereferencing. Passing by reference should not be slower than anything else because there isn't any work done by the CPU in passing by reference that wouldn't have to be done to pass by value or pass a pointer. (Passing by reference is passing a memory address, but to pass by value you still need the address because it has to be dereferenced to get the value, which could be slow, so if anything I'd say passing by value could be slower) Of course, there can be exceptions if one thing or the other already happens to be in a register; I'm just speaking generally.
The point of passing a pointer by reference is to modify the value of the pointer in the function without having to deal with a second layer of pointers, not to save time or memory. Well, that's always the point of references, to do the same basic set of tasks as with pointers but without the possibility of pointer errors.
__________________
The spice must flow... "My friends I will have an energy policy that we will be talking about, which will eliminate our dependence on oil from the Middle East that will – that will then prevent us – that will prevent us from having ever to send our young men and women into conflict again in the Middle East." --John McCain May 2nd 2008 Last edited by n0nsensical; 07-04-2005 at 09:26 PM. |
|
|
|
![]() |
| Bookmarks |
| Tags |
| passing, pointers, reference |
| Thread Tools | |
|
|