call by address vs. call by value

J

Joona I Palaste

hasho said:
Why is "call by address" faster than "call by value"?

It's not necessarily. It could be, but then it could be not. The C
standard does not define anything about what is faster than what. It is
strictly a QoI issue only.
 
L

lallous

*Ideally* call by address is faster because call by value makes a new copy
of the argument everytime you call the function.
Call by address does no copies instead pass the address of the variable as
is.

So basically the speed issue comes from the overhead produced by making
copies of the variables.
 
E

Erik

On 10 Feb 2004 04:35:52 -0800, the right honourable
Why is "call by address" faster than "call by value"?

think of calling a hughe data structure by value...

And, btw. the two methods are not interchangeable without penalty, as
may be suggested by your question. they are completely different in
nature.

frgr
Erik
 
R

Richard Bos

[ Don't top-post, please. ]
*Ideally* call by address is faster because call by value makes a new copy
of the argument everytime you call the function.
Call by address does no copies instead pass the address of the variable as
is.

That is the theory, based on a very restricted view of a rather
old-fashioned kind of implementation, with no arguments passed in
registers and rather larger objects than pointers. Actual practice
doesn't always agree with this theory.
In fact, in practice, which objects will be faster passed as pointers
and which are faster passed directly depends entirely on the
architecture, the compiler, and possibly even on the generation of the
architecture (i.e., it could differ between a Pentium II and a Pentium
III). The only way to know which is best for you is to measure, and not
to assume that your measurements are correct for others.
Even then, in most cases the difference between pointer and value
passing is likely to be dwarfed by algorithmic choices. If passing a
pointer makes calling the function slightly faster, but actually doing
anything inside the function slower, more complicated, and more error-
prone, only a fool uses the pointer. Ditto vice versa.

Richard
 
T

Thomas Stegen CES2000

lallous said:
*Ideally* call by address is faster because call by value makes a new copy
of the argument everytime you call the function.
Call by address does no copies instead pass the address of the variable as
is.

Call by address does at least one copy. The address needs to be copied
last time I checked.
So basically the speed issue comes from the overhead produced by making
copies of the variables.

The speed issue comes from the fact that with an address less might
need to be copied. Consider passing structures for example.
 
G

Grumble

Joona said:
It's not necessarily. It could be, but then it could be not. The C
standard does not define anything about what is faster than what.
It is strictly a QoI issue only.

I thought that C defined only call by value, and that call by address
was merely mimicked by passing the value of a pointer to an object.

Perhaps I misunderstood?
 
J

Joona I Palaste

I thought that C defined only call by value, and that call by address
was merely mimicked by passing the value of a pointer to an object.
Perhaps I misunderstood?

Well, technically you are right. I understood hasho's "call by address"
to mean "passing address with call by value".
 
D

Dik T. Winter

>
> Call by address does at least one copy. The address needs to be copied
> last time I checked.
>
>
> The speed issue comes from the fact that with an address less might
> need to be copied. Consider passing structures for example.

On the other hand call by address might mean that more has to be copied.
Consider passing structures by address for example, and you use each
field many times.
 
L

lallous

Dik T. Winter said:
On the other hand call by address might mean that more has to be copied.
Consider passing structures by address for example, and you use each
field many times.
http://www.cwi.nl/~dik/

Hello

Dik and Thomas, I didn't understand your point?

When calling by address the only thing that is being copied is the address
of the variable that you're accessing by address.
That applies to structures or any other variable.

Whereas when calling by value, a copy of the structure will be made before.
 
L

Leor Zolman

I thought that C defined only call by value, and that call by address
was merely mimicked by passing the value of a pointer to an object.

Perhaps I misunderstood?

It is not clear to me that C "defines" call by /anything/, as the only
place I could locate the term "call by value" in the Standard was in
the index, referring to section 6.5.2.2, but the term itself doesn't
even seem to be used in that section.

Colloquially, I usually use the terms "pass by value" and "pass by
name" (or reference, or pointer...although using "reference" can be
problematic in C++) /in close conjunction/ with the object being
passed that way. For example:

int i, j;
foo(i); // pass i by value
bar(&i); // pass i by reference

Perhaps your question is related to the dual nature of the call to
bar; it can be described as "passing i by reference", and also as
"passing a pointer to i by value".
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
D

Dik T. Winter

> news:[email protected]... ....
>
> When calling by address the only thing that is being copied is the address
> of the variable that you're accessing by address.
> That applies to structures or any other variable.
>
> Whereas when calling by value, a copy of the structure will be made before.

But consider what it takes to pick up the value of a field within a routine
with the two passing modes.
 
T

Thomas Stegen CES2000

Dik said:
On the other hand call by address might mean that more has to be copied.
Consider passing structures by address for example, and you use each
field many times.

You have to explain this further, because on the face of it, it
makes no sense.

Why do you have to copy less if you have a copy as opposed to a
reference (or pointer)? Are you talking about moving things between
registers? Memory locations?
 
J

j

Leor Zolman said:
It is not clear to me that C "defines" call by /anything/, as the only
place I could locate the term "call by value" in the Standard was in
the index, referring to section 6.5.2.2, but the term itself doesn't
even seem to be used in that section.

The standard defines ``pass by value''
Look up what ``value of the expression'' means
Colloquially, I usually use the terms "pass by value" and "pass by
name" (or reference, or pointer...although using "reference" can be
problematic in C++) /in close conjunction/ with the object being
passed that way. For example:

int i, j;
foo(i); // pass i by value
bar(&i); // pass i by reference

In both cases, a value is passed. That value in-turn
becomes the value of another container.

``pass by reference'' does not exist in C.
Passing by reference is an entirely different
mechanism in which we pass an actual object to
a function. We then are able to perform operations
upon the passed object as though it were local to
said function.

People commonly mix up ``pass by reference''
with ``pass a reference''

We can pass something we can consider to be a reference
and refer to it as ``pass a reference'' but we can't
use ``pass by reference'' -- they don't mean the same thing.
It is also unneeded when arguments to functions can be
described by a single term ``pass by value'' without the need
of added complexity. -- I find that beginners of C are so
horribly confused because of this.

People will also make the claim that we can emulate
``passing by reference'' by passing pointers but
it isn't all that meaningful(and not to mention, added
complexity). The gross effects can be simulated but
the operation not. The same can also be said for
implementing functions with goto and an explicit stack.

To OP:
Don't use ``call-by-address''. It is such a horribly muddled
term.
call-by-address: call a function by an address
call-by-value: call a function by a value

It _describes_ a way how functions are called as
opposed to how arguments are passed to the function
being called.
 
M

Mark McIntyre

Dik T. Winter said:
Dik and Thomas, I didn't understand your point?

When calling by address the only thing that is being copied is the address
of the variable that you're accessing by address.

I think Dik's point is that with call by address, you have to jump to that
address to get the data. If that data happened to be out of segment, or in
paged memory, you could get a bad performance hit.

The other point is that most architectures have registers. Passing values
in registers is generally way faster than using pointers.
Whereas when calling by value, a copy of the structure will be made before.

Which is a one-time hit.

So the real answer is "it depends, go test it in your situation".

For what its worth, I've found situations where pass by value, even of huge
structures, was way more performant. No idea why, but OpenVMS on AXP
hardware seemed to perfer that.
 
E

E. Robert Tisdale

hasho said:
Why is "call by address" faster than "call by value"?

You probably mean that
"pass by reference" is faster than "pass by value".

In C, "pass by reference" is accomplished
by passing a pointer by value.

Pass by value requires a copy.
If the object is much larger than a pointer,
it is "faster" to pass a pointer than to pass the object
but, of course, referencing the object through a pointer
usually imposes a slight overhead.
They "break over point" depends upon the details
of the machine architecture and the compiler implementation.
 
M

Malcolm

hasho said:
Why is "call by address" faster than "call by value"?
This may be true for structures.

The reason is that passing a structure on the stack requires several copy
operations to copy each member to the stack, whilst passing the address of
the structure requires a single copy of the address to a register. Generally
the first few arguments of a function will be passed in registers, the
remainder, including large structures, on the stack.
 
G

glen herrmannsfeldt

Thomas Stegen CES2000 wrote:

(snip)
Call by address does at least one copy. The address needs to be copied
last time I checked.
(snip)

The speed issue comes from the fact that with an address less might
need to be copied. Consider passing structures for example.

For languages with static allocation the argument lists
can be static, in which case no copying needs to be done.

While I suppose a C program could be written with only static
variables, that would be a rare case.

It is most likely machine dependent which is faster.

-- glen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top