Thread: passing by address vs passing by reference

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    passing by address vs passing by reference

    I'm learning basic C++ and would like to know what the differents are for using passing by reference instead passing by address. Is suppose to be like one of many features that C++ improves on that C had?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    they are both considered "passing by reference" but by the way you worded it I think you mean using references instead of pointers.

    A reference is a pointer but works more like an alias instead. You can only set what it points to when you create it and it can't be changed afterwards. Everytime you use it you don't have to explicitly dereference it -- you treate it as though it were an object rather than a pointer to an object.

    Another benefit of it is that when you initialize it, you don't explicitly use the address of operator, the address is taken implicitly. Example:

    int a;

    int& ref = a;

    ref = 4;

    now 'a' is set to 4.

    That also means that when you pass a variable to a function that has a reference as an argument, you don't even have to be concerned (in most cases) whether it takes a reference or an object since you can pass it with the same syntax.

    So basically the benefit of a reference is that you can work with the data being pointed to without having to explicitly derefrence it all the time. It makes for less operators and code that's easier to read.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Reference is faster than pointer. In general, use const reference when possible.

    Kuphryn
    Last edited by kuphryn; 01-08-2003 at 02:49 PM.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    17

    ref vs. pointers

    I have a question on this subject.

    If you have an array of pointers to objects on the free store. You make sure that none of the pointers are NULL and if the address of these objects will never change. Is it ok to pass references to these objects between functions instead of pointers? Would this improve performance any at all or just the readability?
    -----------------------
    R. D. Nichols, Jr.

    -Why do I have to go EVERYWHERE in this handbasket?

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    17

    oops

    Please ignore the performance part of that question, I just noticed the previous reply....
    -----------------------
    R. D. Nichols, Jr.

    -Why do I have to go EVERYWHERE in this handbasket?

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by kuphryn
    Reference is faster than pointer. In general, use const reference when possible.

    Kuphryn
    That's misinformation. A reference is not faster than a pointer, they are exactly the same in terms of performance.

    There is literally no performance difference between using a pointer vs a reference -- it's just that a reference is implicitly dereferenced rather than explicitly. There is no such thing as a "const reference." The value of a reference can never change -- in that sense, they are always "constant" and you can never change that. If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant. References just simplify the use.

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    "If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant."

    There is indeed a logical difference.

    Pointers can be null, references cannot.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    References are no slower than pointers, but in some cases, your compiler _MAY_ optomize away references. It's best to forget that you knew this, though there is a nifty trick that lets you turn

    a = b+c*d;
    into
    a.plus_times(b,c,d);

    Never use a reference to access something that you can normally only get to with a pointer, the exception being returning *this in a method that returns a reference. Never have references to something on the heap, i.e.

    foo &f = *(new foo(x,y));
    ...
    f.no_arrow_for_me();
    ...
    delete &f;

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by SilentStrike
    "If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant."

    There is indeed a logical difference.

    Pointers can be null, references cannot.
    I was refering to a dereferenced constant pointer -- otherwise there are many logical differences.

  10. #10
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by Polymorphic OOP
    There is no such thing as a "const reference." The value of a reference can never change -- in that sense, they are always "constant" and you can never change that. If he meant using a reference to a constant, there will be no performance gain or logical change from using a constant pointer to a constant. References just simplify the use.
    Huh, Poly? What about this?
    Code:
    int some_function(const &value);
    That's what I was taught was a constant reference parameter. The advantage to including 'const' is basically that you can't change the value being passed to the function. If one tries to change it, one will get a compiler error. I personally think it also makes the code easier to read, for the const says that that argument won't be changed in that function. I think that's what Kuphryn meant by a 'const reference'.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Huh, Poly? What about this?

    Code:
    int some_function(const &value);

    Nope, thats a reference to a const, not a const reference.

    Examples:

    int* a; // pointer to an int
    const int* a; // pointer to a const int
    int* const a; // const pointer to an int
    const int* const a;// const pointer to a const int

    now with references:

    int& a; // reference to an int
    const int& a; // reference to a constant int
    int& const a; // Nope, nothin, sorry
    const int& const a; // Same here

    A reference is always constant, but a "const reference" is not defined.
    Last edited by Polymorphic OOP; 01-08-2003 at 11:12 PM.

  12. #12
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Const reference is pretty popular term, occurs about 16 times more than reference to const.

    http://www.google.com/search?hl=en&l...=Google+Search
    http://www.google.com/search?hl=en&l...=Google+Search
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  13. #13
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Just something that many nitpick about...

    void f(int* a) { }
    Technically is pass by value. In fact, every kind of parameter
    passing in c is pass by value except inline functions using
    #define. C++ has pass by referance though.

  14. #14
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    All depends on the concept of the pass.

    You can look at your example as passing the value by reference OR you can look at it as passing the pointer by value. Passing by reference doesn't imply actually using a C++ reference type.

  15. #15
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    It does not imply only because everyone botches
    the definition of passing by referance. Even if you
    pass an array to a function the array is decayed to
    a pointer that is passed by value.
    http://www.eskimo.com/~scs/C-faq/q4.11.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  3. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM