call by address vs. call by value

Discussion in 'C Programming' started by hasho, Feb 10, 2004.

  1. hasho

    hasho Guest

    Why is "call by address" faster than "call by value"?
     
    hasho, Feb 10, 2004
    #1
    1. Advertising

  2. hasho <> scribbled the following:
    > 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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Bad things only happen to scoundrels."
    - Moominmamma
     
    Joona I Palaste, Feb 10, 2004
    #2
    1. Advertising

  3. hasho

    lallous Guest

    *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.

    --
    Elias
    "hasho" <> wrote in message
    news:...
    > Why is "call by address" faster than "call by value"?
     
    lallous, Feb 10, 2004
    #3
  4. hasho

    Erik Guest

    On 10 Feb 2004 04:35:52 -0800, the right honourable
    (hasho) wrote:

    >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
     
    Erik, Feb 10, 2004
    #4
  5. hasho

    Richard Bos Guest

    "lallous" <> wrote:

    [ Don't top-post, please. ]

    > "hasho" <> wrote in message
    > news:...
    > > Why is "call by address" faster than "call by value"?

    > *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
     
    Richard Bos, Feb 10, 2004
    #5
  6. lallous wrote:
    > *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.

    --
    Thomas.
     
    Thomas Stegen CES2000, Feb 10, 2004
    #6
  7. hasho

    Grumble Guest

    Joona I Palaste wrote:

    > hasho wrote:
    >
    >> 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.


    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?
     
    Grumble, Feb 10, 2004
    #7
  8. Grumble <> scribbled the following:
    > Joona I Palaste wrote:
    >> hasho wrote:
    >>> 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.


    > 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".

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "I wish someone we knew would die so we could leave them flowers."
    - A 6-year-old girl, upon seeing flowers in a cemetery
     
    Joona I Palaste, Feb 10, 2004
    #8
  9. In article <4028e5bb$> Thomas Stegen CES2000 <> writes:
    > lallous wrote:
    > > *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.


    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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Feb 10, 2004
    #9
  10. hasho

    lallous Guest

    "Dik T. Winter" <> wrote in message
    news:...
    > In article <4028e5bb$> Thomas Stegen CES2000

    <> writes:
    > > lallous wrote:
    > > > *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.

    >
    > 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.
    > --
    > dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland,

    +31205924131
    > home: bovenover 215, 1025 jn amsterdam, nederland;

    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.

    --
    Elias
     
    lallous, Feb 10, 2004
    #10
  11. hasho

    Leor Zolman Guest

    On Tue, 10 Feb 2004 15:23:18 +0100, Grumble <>
    wrote:

    >Joona I Palaste wrote:
    >
    >> hasho wrote:
    >>
    >>> 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.

    >
    >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

    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
     
    Leor Zolman, Feb 10, 2004
    #11
  12. In article <c0atvo$150h6e$-berlin.de> "lallous" <> writes:
    > "Dik T. Winter" <> wrote in message
    > news:...

    ....
    > > 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.

    >
    > 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.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
     
    Dik T. Winter, Feb 10, 2004
    #12
  13. hasho

    j Guest

    "hasho" <> wrote in message
    news:...
    > Why is "call by address" faster than "call by value"?


    What do you think ``call-by-address'' is?
     
    j, Feb 10, 2004
    #13
  14. Dik T. Winter wrote:
    > In article <4028e5bb$> Thomas Stegen CES2000 <> writes:
    > > 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.


    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?

    --
    Thomas.
     
    Thomas Stegen CES2000, Feb 10, 2004
    #14
  15. hasho

    j Guest

    "Leor Zolman" <> wrote in message
    news:...
    > On Tue, 10 Feb 2004 15:23:18 +0100, Grumble <>
    > wrote:
    >
    > >Joona I Palaste wrote:
    > >
    > >> hasho wrote:
    > >>
    > >>> 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.

    > >
    > >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.
    >


    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.

    > 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
    >
    > 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
     
    j, Feb 10, 2004
    #15
  16. On Tue, 10 Feb 2004 17:33:36 +0200, in comp.lang.c , "lallous"
    <> wrote:

    >"Dik T. Winter" <> wrote in message
    >news:...
    >> In article <4028e5bb$> Thomas Stegen CES2000


    >> 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.


    >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.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Feb 10, 2004
    #16
  17. hasho wrote:

    > 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.
     
    E. Robert Tisdale, Feb 10, 2004
    #17
  18. hasho

    Malcolm Guest

    "hasho" <> wrote in message
    >
    > 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.
     
    Malcolm, Feb 10, 2004
    #18
  19. In article <>,
    (hasho) wrote:

    > Why is "call by address" faster than "call by value"?


    What is "call by address"? Could you explain?
     
    Christian Bau, Feb 10, 2004
    #19
  20. 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
     
    glen herrmannsfeldt, Feb 10, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    1
    Views:
    7,335
    avnrao
    May 4, 2004
  2. Patrice
    Replies:
    0
    Views:
    923
    Patrice
    May 4, 2004
  3. Raterus
    Replies:
    0
    Views:
    463
    Raterus
    Jun 2, 2004
  4. Alex Stevens
    Replies:
    0
    Views:
    805
    Alex Stevens
    Aug 10, 2004
  5. =?Utf-8?B?QW5nZWw=?=

    Pass a Web Address and return a File System Address

    =?Utf-8?B?QW5nZWw=?=, Sep 20, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    557
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=
    Sep 21, 2005
Loading...

Share This Page