06-20-2005, 10:56 AM | #1 (permalink) |
Junkie
|
[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; } |
06-20-2005, 06:26 PM | #3 (permalink) |
Upright
|
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.
|
07-04-2005, 08:31 AM | #4 (permalink) |
Upright
|
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.
|
07-04-2005, 07:23 PM | #5 (permalink) |
Junkie
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.
__________________
"Prohibition will work great injury to the cause of temperance. It is a species of intemperance within itself, for it goes beyond the bounds of reason in that it attempts to control a man's appetite by legislation, and makes a crime out of things that are not crimes. A Prohibition law strikes a blow at the very principles upon which our government was founded." --Abraham Lincoln Last edited by n0nsensical; 07-04-2005 at 07:26 PM.. |
Tags |
passing, pointers, reference |
|
|