473,785 Members | 2,612 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13181
* 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
3095
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 over the following code whether its pass by reference or pass by value...hope somebody clears it up :-) void foo(char *s) { s = new char; strcpy(s, "gotit"); }
110
9963
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 must be an object instead of
4
3409
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
4
2293
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. My interpretation: 1. Essentially pushes the value type on the stack
14
20412
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
10
13663
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 function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
6
2714
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
11
3364
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 ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
15
2026
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
6689
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 pass-by-reference to the first function, addStudent() but then I am confused as to how I am suppose to pass the pointer of 'student' from within the addStudent() function to the getAge() function that is nested within the addStudent() function.
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.