Re: Writing Efficient Arguments
On Jul 10, 12:41 pm, Michael DOUBEZ <michael.dou...@free.fr> wrote:
gpuchtel a =E9crit :
On Jul 9, 7:03 pm, spekyu...@gmail.com wrote:
Pointer and reference arguments provide C++ programmers with the
ability to modify objects. What is more efficient, passing arguments
via pointer or reference? Avoid the stereotypical urge of debating
effective coding style. Instead, think of particular scenarios:
passing a 512MB object to function. Think behind the scenes, from the
stack and heap to the physical linking and build of the program.
[snip]
The 'efficiency' is in the recipient. That is, it does not have to
check if a reference is null - since a reference can never be null.
A reference can be NULL or invalid all the same.
Only if the code contains undefined behavior, and even then, who
knows. Generally speaking, there is no way to get a null
reference without incurring undefined behavior, and once you've
incurred undefined behavior, there are no guarantees concerning
what you get.
The difference is that the code indicates a NULL wouldn't be
expected (i.e. the code is self explaining; whereas with
pointer, you would have to read the doc/comments).
AFAIK the following code is valid (though UB):
So which is it? Valid, or undefined behavior? It can't be
both.
void print(int& i)
{
cout<<&i<<endl;
}
int main()
{
print(*static_cast<int*>(0));
return 0;
}
And prints '0'.
Maybe. On some implementation. Some days of the month. I've
definitly used an implementation where it would core dump.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34