473,500 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

f(int& i) vs. f(int i)

Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool, etc.)
Jul 22 '05 #1
12 2099
jeffc wrote:
Can anyone point me to a web site that gives technical reasons that
pass by value is preferred over pass by reference when the value is
not to be changed? Actually it could be any built-in "small" type
(short, bool, etc.)


Technical reasons? I guess there are none... I mean, if you pass a "small
type," as you call it, you are passiing either the value itself on the
stack (if pass by value), or you are passing a pointer to it (if pass by
reference), both of which are probably the same size (ie, a pointer might
be 32 bits, and an integer value might be the same size).

Non-technical reasons? Well, if you pass by value you cannot change the
value within the routine for sure, and of course, the discussion can get
much more involved on the non-technical aspects (ie, const, etc...).

--
gabriel
Jul 22 '05 #2
jeffc wrote:
Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool, etc.)


Let's see:

int pass_by_reference( int & a, int & b )
{
return a + b;
}
int pass_by_value( int a, int b )
{
return a + b;
}
passing_params.o: file format elf32-i386

Disassembly of section .text:

00000000 <_Z17pass_by_referenceRiS_>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 0c mov 0xc(%ebp),%eax
6: 8b 00 mov (%eax),%eax
8: 8b 55 08 mov 0x8(%ebp),%edx
b: 03 02 add (%edx),%eax
d: c9 leave
e: c3 ret
f: 90 nop

00000010 <_Z13pass_by_valueii>:
10: 55 push %ebp
11: 89 e5 mov %esp,%ebp
13: 8b 45 0c mov 0xc(%ebp),%eax
16: 03 45 08 add 0x8(%ebp),%eax
19: c9 leave
1a: c3 ret

Which one do you think is faster ?

Try a passing parameters in registers calling convention (SPARC).

_Z17pass_by_referenceRiS_:
ld [%o0], %g1
ld [%o1], %o2
retl
add %g1, %o2, %o0
_Z13pass_by_valueii:
retl
add %o0, %o1, %o0

Now which one do you think is faster ?

The true answer is - it depends. A simple function like these are
easily candidates for inlining - however if these are not inlineable
then passing somthing that can reside in a register by value rather than
by reference can have performance gains due to not needing to
dereference. Also, it depends on the circumstances of the caller, if
the caller does not have the values in registers it may be less
expensive computationally to pass the pointers instead of dereferencing
in the caller.

The common wisdom however is - the fewer instructions, the faster the
code. The correct thing to do is to though is benchmark your code. In
very few circumstances will this really make a difference.

Jul 22 '05 #3
Gianni Mariani wrote:
Disassembly of section .text: [...]
Don't forget, though, that there are many variables here, such as
optimizing compilers, (as you mention) inlines, crappy compilers, and
hardware differences, none of which can be controlled by the source code
alone. For example, the compiler may dereference the pointer only once
at the beginning of the routine, use a temp value through the routine,
and then dereference the pointer again at the end to put the value back.
Though doing this would wreak havoc on multi threaded systems that access
the same pointers simultaneously, I can definitely see this being done by
a compiler coder tryin' to be fast...

The common wisdom however is - the fewer instructions, the faster the
code. The correct thing to do is to though is benchmark your code.
I would say that this is yesterday's common wisdom. Optimizers throw a
wrench into this nowadays, so very compact and obfuscated code does not
necessarily guarantee fast or efficient execution.

Unless you use an old compiler and you are very familiar with what
assembler code is generated by which source code instructions, you are
better off speding time optimizing your _algorithms_ and letting the
modern compiler figure out which assembler code to generate.
In very few circumstances will this really make a difference.


Very, very true. Time is better spent somewhere else the vast majority
of the time.

--
gabriel
Jul 22 '05 #4
"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
Can anyone point me to a web site that gives technical reasons that pass by value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool,

etc.)

void scale(vector<double>& v, double& factor)
{
for (vector<double>::iterator it = v.begin(); it != v.end(); ++it)
*it *= factor;
}

If you have a vector<double> named, say, x, and you call scale(x, x[0]), the
roof caves in.

If factor is double rather than double&, all is well.
Jul 22 '05 #5

"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
Can anyone point me to a web site that gives technical reasons that pass by value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool,

etc.)

Please let me reiterate: I'm not looking for answers per se. I'm looking
for a WEB SITE that provides this sort of information. Thanks.
Jul 22 '05 #6
jeffc wrote:
"jeffc" <no****@nowhere.com> wrote in message
news:40********@news1.prserv.net...
Can anyone point me to a web site that gives technical reasons that pass


by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool,


etc.)

Please let me reiterate: I'm not looking for answers per se. I'm looking
for a WEB SITE that provides this sort of information. Thanks.


What people are saying is that your answers are compiler and
platform dependent. One compiler could use registers to pass
by value and reference, thus making them both equal.

The primary difference is mutable. Passing by value is passing
a copy of parameter. Passing by reference allows the original
variable to be modified. If one is to compare apples to apples
the one should compare a constant reference to pass by value.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #7

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Ci******************@newssvr16.news.prodigy.c om...

Please let me reiterate: I'm not looking for answers per se. I'm looking for a WEB SITE that provides this sort of information. Thanks.

What people are saying is that your answers are compiler and
platform dependent. One compiler could use registers to pass
by value and reference, thus making them both equal.

The primary difference is mutable. Passing by value is passing
a copy of parameter. Passing by reference allows the original
variable to be modified. If one is to compare apples to apples
the one should compare a constant reference to pass by value.


Thomas, I understand. I was hoping someone might know off the top of their
heads where I could find something like your description, or anyone's
description, preexisting on a web site. In other words, if you know of a
web site (could be a FAQ, or any other instructional or informational web
site) that says it is compiler or platform dependent, or that the primary
difference is mutable, or that it makes little difference in practice, or
whatever. I was not able to find a site with such info in a search. Again,
the answers have been helpful, but I'm not looking for the answers. I'm
just looking for web sites that mention the topic. thanks for your reply.
Jul 22 '05 #8
jeffc wrote:
Please let me reiterate: I'm not looking for answers per se. I'm looking
for a WEB SITE that provides this sort of information. Thanks.


Jeez, do we gotta even do a google search for you? Just search through
groups.google.com for this thread and there is your web reference...

--
gabriel
Jul 22 '05 #9

"gabriel" <no@no--spam.com> wrote in message
news:be***************************@msgid.meganewss ervers.com...
jeffc wrote:
Please let me reiterate: I'm not looking for answers per se. I'm looking for a WEB SITE that provides this sort of information. Thanks.


Jeez, do we gotta even do a google search for you? Just search through
groups.google.com for this thread and there is your web reference...


Obviously that would defeat the purpose of a *preexisting* reference. Look,
obviously I was unclear clear about the purpose - let's just drop it.
Thanks for the replies.
Jul 22 '05 #10
gabriel wrote:
Gianni Mariani wrote:
The common wisdom however is - the fewer instructions, the faster the
code. The correct thing to do is to though is benchmark your code.


I would say that this is yesterday's common wisdom. Optimizers throw a
wrench into this nowadays, so very compact and obfuscated code does not
necessarily guarantee fast or efficient execution.


I believe Gianni meant machine-language instructions, not lines of C++ code.

There's a great section in the Josuttis templates book on defining
functions that take arguments by value or reference, depending on a
number of factors.

Jul 22 '05 #11
"jeffc" <no****@nowhere.com> wrote in message news:<40********@news1.prserv.net>...
Can anyone point me to a web site that gives technical reasons that pass by
value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool, etc.)


Check out http://www.boost.org/libs/utility/call_traits.htm
Jul 22 '05 #12

"Dan McLeran" <da***********@seagate.com> wrote in message
news:19**************************@posting.google.c om...
"jeffc" <no****@nowhere.com> wrote in message news:<40********@news1.prserv.net>...
Can anyone point me to a web site that gives technical reasons that pass by value is preferred over pass by reference when the value is not to be
changed? Actually it could be any built-in "small" type (short, bool,

etc.)
Check out http://www.boost.org/libs/utility/call_traits.htm


ok thanks
Jul 22 '05 #13

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

Similar topics

5
2931
by: Philip D Heady | last post by:
Anyone know where i can find all world currencies (USD,etc) and do an import into mySQL? Just need a comma delimited list if possible. Example: USD, United States Dollar etc, etc
4
6025
by: marco_segurini | last post by:
Hi, I am wondering if there is a way to use the ternary operator to select a function, between two, even if at least one of them is overloaded. Ex: void f1(int) {} void f1(double) {} void...
0
411
by: Engin H. | last post by:
Hi all I have a ASP.NET web project and i develop a Windows Control Library, which references Excel interop, in my web project. In my computer it works ok, when i deploy the project to the server,...
13
2153
by: Elaine | last post by:
This has to do with self-joins and includes a running balance problem. Is it possible to do this using SQL alone? Amortization schedule -------------------------- Givens: beginning balance,...
5
1671
by: Zanzibar | last post by:
H Is it possible that the Personal edition of C#, provided for free with the book Study C# Deluxe, produces very slow code? When I write a loop at home that creates a 100,000 objects, it takes...
9
1683
by: Klaas Vantournhout | last post by:
Hi all, I have a question about friends functions of a template class. To make it simple, I would like to do something like this. Assume that I have a class foo with template T ...
11
2757
by: Dennis Jones | last post by:
Hello, I would like to know if there is a known pattern for creating a map of reference-counted objects, such that when the last reference to an object is deleted, the element referring to that...
4
2234
by: Doug Gray | last post by:
Folks, I am looking for a fast but most importantly a bullet proof method to pass and NMEA data stream (GPS output) ascii numeric strings. The best I can offer is: def fint(a): try: return...
206
13085
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
1
1371
by: yuan | last post by:
halo.. i face problem when i execute the programming. it appear the error as : 1.obj : error LNK2001: unresolved external symbol "double __cdecl NR::trapzd(double...
0
7136
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,...
0
7182
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,...
0
7232
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...
1
6906
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...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
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,...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
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 ...

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.