473,378 Members | 1,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

pass by reference vs pass by pointer

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

Sep 22 '05 #1
6 13156
* 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?
Sep 22 '05 #2
LuB
Thanks Alf!

-LuB

Sep 22 '05 #3
> 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
Sep 22 '05 #4
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

Sep 22 '05 #5
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.
Sep 22 '05 #6

"Default User" <de***********@yahoo.com> wrote in message
news:3p************@individual.net...
Stuart MacMartin wrote:
Nicely stated.

What is?


What's on second.

-Mike
Sep 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: dumboo | last post by:
hi there i m little bit confused over the following problem, i have understood wt the following code is doing...but not able to get the actual techinical stuff.....i have had a lot of hot debate...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
10
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
4
by: S. | last post by:
Hi all, I have the requirement that I must pass-by-reference to my function addStudent() and getAge() functions where my getAge() function is within the addStudent() function. I am able to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.