Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pass by reference vs pass by pointer

Reply
Thread Tools

pass by reference vs pass by pointer

 
 
LuB
Guest
Posts: n/a
 
      09-22-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-22-2005
* 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.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
LuB
Guest
Posts: n/a
 
      09-22-2005
Thanks Alf!

-LuB

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      09-22-2005
> 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


 
Reply With Quote
 
Stuart MacMartin
Guest
Posts: n/a
 
      09-22-2005
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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-22-2005
Stuart MacMartin wrote:

> Nicely stated.



What is?



Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-23-2005

"Default User" <> wrote in message
news:...
> Stuart MacMartin wrote:
>
>> Nicely stated.

>
>
> What is?


What's on second.

-Mike


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pass by Pointer * or Pass by Reference &? Robert C++ 10 08-24-2005 04:15 PM
Pass-by-reference instead of pass-by-pointer = a bad idea? Mr A C++ 111 07-14-2005 03:04 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57