473,218 Members | 1,445 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,218 software developers and data experts.

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 2083
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
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
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
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
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
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
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
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
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
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
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
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.