What is the difference between passing to a function by reference and passing by address? They both enable you to modify the variable, so why make two seperate things? Title edit: Passing by reference or address.
What is the difference between passing to a function by reference and passing by address? They both enable you to modify the variable, so why make two seperate things? Title edit: Passing by reference or address.
One reason would be that the use of references simplifies the syntax (no need to take the address when passing and no need to dereference the pointer). Another reason would be that while a pointer parameter can be "optional" in the sense that a null pointer can be passed, a reference parameter would not be optional in that way.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I think you got confused here. There are only two types of passing parameters: Pass-by-refrence and Pass-by-value. Only pass-by-reference allows you to modify the original variable, whereas pass-by-value will modify only the local copy.
Maybe you have a more concrete example of what you're trying to achieve?
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
In machine code they are both the same thing. When using references, you can be sure that the reference itself will be a valid memory address [unless you really jump through hoops to get there, or possibly that you are passing an argument like "*ptr"].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Passing by reference
- Doesn't require the address-of (&) operator when passing a variable
- Doesn't require the dereference (*) operator when changing the value of a reference
- Guarantees the argument will be an object (you can't have a NULL reference)
- Guarantees the reference will always refer to the same object
Pointers are mainly used for memory management, but here's an example to contrast pointers and references:
EDIT: Posted a little too lateCode:void f (int& ref) { ref++; // Pretty } void f (int* ptr) { (*ptr)++; // Ugly } int main() { int var = 3; f (var); // Pass by reference, implicit f (&var); // Pass by pointer, explicit f (NULL); // Oops! Pass by pointer return 0; }
Last edited by rudyman; 09-02-2008 at 09:15 AM.
>>I took it to be the difference between C style pointer passing and C++ style references.
Yes that is what I meant. Thanks for the replies.
I'm not sure, but if we do:
I believe this will pass the address in px (0) to foo, and foo would crash. And you would have to do:Code:void foo(int &x) { x++; } int main() { int *px = 0; foo(*px); }
to avoid that.Code:void foo(int &x) { if (!&x) return; x++; }
[I have just compiled the above (first version) code, and it does indeed crash inside the function foo when compiled with g++ mingw 3.4, whilst second version doesn't crash].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This dereferences a null pointer, so the behaviour is undefined even before it gets into the function.
Passing a reference signals that no checks are necessary (the wrong if at all has already been done). When a pointer is passed, it should probably always be checked for NULL, unless perhaps it is known to always come from a trusted source.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
All I know is that many years ago, during a code review, I suggested that the dev team check if a NULL reference was passed in... Well, they made it a point to have a good laugh by bringing that up as often as possible.
IMO, the thing is that undefined behaviour has already happened before that let you create a NULL reference in the first place, so the test would be too late. You won't test if an array index is out of bounds after you have accessed it.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
On the language standard level, the UB occurs at the moment you do *ptr. At the machine code level of all implementations I know, however, "int& r = *ptr" is a simple copy of the address, so only using the reference will trigger the machine exception.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I agree with CornedBee - I was just trying to point out that a reference can lead to access to incorrect memory, and yes, the fault is in the calling code that doesn't check the pointer before making a reference of it. Unfortunately, that doesn't necessarily help in the debugging when you are several levels of complicated calls down from where the reference was set.
And no, I'm not suggesting that NULL checks are made to references - I just wanted to prove to myself that they can be checked. Checking for NULL on anything that isn't a pointer in the first place is meaningless - consider this:
Address 4 in memory will be the result of this, and it will crash just as much as address zero.Code:struct A { int x; int y; }; A *pa = 0; extern foo(int &x); // Refers to the above foo with NULL check. int main() { foo(pa->y); ... }
Out of bounds memory addresses can be achieved all sorts of different ways, and not only with NULL, so checking for NULL is not really helping - just let it crash if it's not right, and debug the exception. However, make sure that whenever possible, you check the range when indexing and return values from memory allocations!
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.