pass by reference vs pass by pointer

Discussion in 'C++' started by LuB, Sep 22, 2005.

  1. LuB

    LuB Guest

    Hi,

    I wanted to use the most efficient argument passing method. I was
    always taught that its best to pass (const SomeObject& obj) if possible
    .... but in this case, I can't pass a const param since I will be
    modifying the parameter in the function.


    Eg: 1

    void foo(SomeObject& obj)
    {
    obj.doSomething();
    }


    or Eg: 2

    void foo(SomeObject* obj)
    {
    obj->doSomething();
    }


    Aside from the NULL PTR safety of the reference version, is there a
    performance advantage in the way the function and parameters are
    placed/copied onto the stack for either version?

    Thanks,

    -Luther
     
    LuB, Sep 22, 2005
    #1
    1. Advertising

  2. * LuB:
    >
    > I wanted to use the most efficient argument passing method.


    Premature optimization is the root of all evil. And anyway, what you ask for
    here is a bit advanced. If you absolutely want to do it, refer to 'Modern C++
    Design' for a discussion of how to partially automate the selection of how to
    do argument passing, based on size and in/out/in-out requirements.


    > I was
    > always taught that its best to pass (const SomeObject& obj) if possible


    No.


    > ... but in this case, I can't pass a const param since I will be
    > modifying the parameter in the function.
    >
    >
    > Eg: 1
    >
    > void foo(SomeObject& obj)
    > {
    > obj.doSomething();
    > }
    >
    >
    > or Eg: 2
    >
    > void foo(SomeObject* obj)
    > {
    > obj->doSomething();
    > }
    >
    >
    > Aside from the NULL PTR safety of the reference version, is there a
    > performance advantage in the way the function and parameters are
    > placed/copied onto the stack for either version?


    No, but I can imagine the reference version to be easier to optimize (because
    no analysis is needed to check what the reference refers to in each
    statement), so it might be that it's better optimized with some compilers.

    However, the NULL pointer safety is important, and there are other
    non-efficiency related issues.

    One particularly important such issue is that the reference cannot be
    re-seated within the function. Another is that arithmetic cannot be performed
    on the reference, only on the object it refers to (think of e.g. a std::string
    argument). A third is that the reference signature clearly indicates a single
    object, whereas the pointer signature matches object or array or null.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Sep 22, 2005
    #2
    1. Advertising

  3. LuB

    LuB Guest

    Thanks Alf!

    -LuB
     
    LuB, Sep 22, 2005
    #3
  4. LuB

    benben Guest

    > I wanted to use the most efficient argument passing method. I was
    > always taught that its best to pass (const SomeObject& obj) if possible
    > ... but in this case, I can't pass a const param since I will be
    > modifying the parameter in the function.
    >
    >
    > Eg: 1
    >
    > void foo(SomeObject& obj)
    > {
    > obj.doSomething();
    > }
    >
    >
    > or Eg: 2
    >
    > void foo(SomeObject* obj)
    > {
    > obj->doSomething();
    > }
    >
    >
    > Aside from the NULL PTR safety of the reference version, is there a
    > performance advantage in the way the function and parameters are
    > placed/copied onto the stack for either version?
    >
    > Thanks,
    >
    > -Luther
    >


    If the object is small enough it is actually (most of the time) more
    efficient to pass it by value, but that will have a different semantics.

    I personally prefer pointer over reference.

    Ben
     
    benben, Sep 22, 2005
    #4
  5. Nicely stated. I prefer reference over pointer. Not relevant to this
    thread other than to say the compiler supports both use models and
    performance is not a factor in deciding which you use.

    As far as the compiler: surely it's true that almost every compiler
    will generate the same code whether you pass by reference or pass by
    pointer (assuming the C++ code is equivalent). Yes?

    Stuart
     
    Stuart MacMartin, Sep 22, 2005
    #5
  6. LuB

    Default User Guest

    Stuart MacMartin wrote:

    > Nicely stated.



    What is?



    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Sep 22, 2005
    #6
  7. LuB

    Mike Wahler Guest

    "Default User" <> wrote in message
    news:...
    > Stuart MacMartin wrote:
    >
    >> Nicely stated.

    >
    >
    > What is?


    What's on second.

    -Mike
     
    Mike Wahler, Sep 23, 2005
    #7
    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. sam pal
    Replies:
    3
    Views:
    592
    E. Robert Tisdale
    Jul 16, 2003
  2. Mr A
    Replies:
    111
    Views:
    2,181
  3. Robert
    Replies:
    10
    Views:
    1,395
    E. Robert Tisdale
    Aug 24, 2005
  4. Replies:
    4
    Views:
    410
    Andre Kostur
    Dec 24, 2007
  5. A
    Replies:
    7
    Views:
    656
Loading...

Share This Page