You can get the assembly code from your compiler and compare them. At least in GCC, they produce identical code.
views:
638answers:
9Here's the difference in the generated assembly with g++. a.cpp
is pointers, b.cpp
is references.
$ g++ -S a.cpp
$ g++ -S b.cpp
$ diff a.S b.S
1c1
< .file "a.cpp"
---
> .file "b.cpp"
4,6c4,6
< .globl __Z7MyFunc1PiS_
< .def __Z7MyFunc1PiS_; .scl 2; .type 32; .endef
< __Z7MyFunc1PiS_:
---
> .globl __Z7MyFunc1RiS_
> .def __Z7MyFunc1RiS_; .scl 2; .type 32; .endef
> __Z7MyFunc1RiS_:
Just the function name is slightly different; the contents are identical. I had identical results when I used g++ -O3
.
My rule of thumb is to pass by pointer if the parameter can be NULL, ie optional in the cases above, and reference if the parameter should never be NULL.
References are very similar to pointers with one big difference: references can not be NULL. So you no not need to check if they are acutual usable objects (like for pointers).
Therefore I assume that compilers will produce the same code.
From a performance point of view, it probably doesn't matter. Others have already answered that.
Having said that, I have yet not found a situation where an added instruction in this case would make a noticeable difference. I do realize that for a function that is called billions of times, it could make a difference. As a rule, you shouldn't adapt your programming style for these kind of "optimizations".
This will get voted down since it's Old Skool, but I often prefer pointers since it's easier to just glance at code and see if my objects that I am passing to a function could get modified, especially if they are simple datatypes like int and float.
You should see the generated assembly code for the target machine... take into account that a function call is always done in constant time, and on actual machines that time is really negligible...
All the other answers already point out that neither function is superior to the other in terms of runtime performance.
However, I think that that the former function is superior to the other in terms of readability, because a call like
f( &a, &b );
clearly expresses that a reference to some variable is passed (which, to me, rings the 'this object might be modified by the function' bell). The version which takes references instead of pointers just looks like
f( a, b );
It would be fairly surprising to me to see that a
changed after the call, because I cannot tell from the invocation that the variable is passed by reference.
There are different guidelines on using reference vs. pointer parameters out there, tailored to different requirements. In my opinion, the most meaningful rule that should be applied in generic C++ development is the following:
Use reference parameters when overloading operators. (In this case you actually have no choice. This is what references were introduced for in the first place.)
Use const-reference for composite (i.e. logically "large") input parameters. I.e input parameters should be passed either by value ("atomic" values) or by const-reference ("aggregate" values). Use pointers for output parameters and input-output parameters. Do not use references for output parameters.
Taking the above into the account, the overwhelming majority of reference parameters in your program should be const-references. If you have a non-const reference parameter and it is not an operator, consider using a pointer instead.
Following the above convention, you'll be able to see at the point of the call whether the function might modify one of its arguments: the potentially modified arguments will be passed with explicit &
or as already-existing pointers.
There's another popular rule out there that states that something that can be null should be passed as a pointer, while something that can't be null should be passed as a reference. I can imagine that this might make sense in some very narrow and very specific circumstances, but in general this is a major anti-rule. Just don't do it this way. If you want to express the fact that some pointer must not be null, put a corresponding assertion as the very first line of your function.
As for the perfromance considerations, there's absolutely no performance difference in passing by pointer or passing by reference. Both kinds of parameters are exactly the same thing at the physical level. Even when the function gets inlined, a modern compiler should be smart enough to preserve the equivalence.