pass by reference vs pass by pointer

L

LuB

Hi,

I wanted to use the most efficient argument passing method. I was
always taught that its best to pass (const SomeObject& obj) if possible
.... but in this case, I can't pass a const param since I will be
modifying the parameter in the function.


Eg: 1

void foo(SomeObject& obj)
{
obj.doSomething();
}


or Eg: 2

void foo(SomeObject* obj)
{
obj->doSomething();
}


Aside from the NULL PTR safety of the reference version, is there a
performance advantage in the way the function and parameters are
placed/copied onto the stack for either version?

Thanks,

-Luther
 
A

Alf P. Steinbach

* LuB:
I wanted to use the most efficient argument passing method.

Premature optimization is the root of all evil. And anyway, what you ask for
here is a bit advanced. If you absolutely want to do it, refer to 'Modern C++
Design' for a discussion of how to partially automate the selection of how to
do argument passing, based on size and in/out/in-out requirements.

I was
always taught that its best to pass (const SomeObject& obj) if possible
No.


... but in this case, I can't pass a const param since I will be
modifying the parameter in the function.


Eg: 1

void foo(SomeObject& obj)
{
obj.doSomething();
}


or Eg: 2

void foo(SomeObject* obj)
{
obj->doSomething();
}


Aside from the NULL PTR safety of the reference version, is there a
performance advantage in the way the function and parameters are
placed/copied onto the stack for either version?

No, but I can imagine the reference version to be easier to optimize (because
no analysis is needed to check what the reference refers to in each
statement), so it might be that it's better optimized with some compilers.

However, the NULL pointer safety is important, and there are other
non-efficiency related issues.

One particularly important such issue is that the reference cannot be
re-seated within the function. Another is that arithmetic cannot be performed
on the reference, only on the object it refers to (think of e.g. a std::string
argument). A third is that the reference signature clearly indicates a single
object, whereas the pointer signature matches object or array or null.
 
B

benben

I wanted to use the most efficient argument passing method. I was
always taught that its best to pass (const SomeObject& obj) if possible
... but in this case, I can't pass a const param since I will be
modifying the parameter in the function.


Eg: 1

void foo(SomeObject& obj)
{
obj.doSomething();
}


or Eg: 2

void foo(SomeObject* obj)
{
obj->doSomething();
}


Aside from the NULL PTR safety of the reference version, is there a
performance advantage in the way the function and parameters are
placed/copied onto the stack for either version?

Thanks,

-Luther

If the object is small enough it is actually (most of the time) more
efficient to pass it by value, but that will have a different semantics.

I personally prefer pointer over reference.

Ben
 
S

Stuart MacMartin

Nicely stated. I prefer reference over pointer. Not relevant to this
thread other than to say the compiler supports both use models and
performance is not a factor in deciding which you use.

As far as the compiler: surely it's true that almost every compiler
will generate the same code whether you pass by reference or pass by
pointer (assuming the C++ code is equivalent). Yes?

Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top