By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.