Passing by refernce of address

This is a discussion on Passing by refernce of address within the C++ Programming forums, part of the General Programming Boards category; What is the difference between passing to a function by reference and passing by address? They both enable you to ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    266

    Passing by refernce of address

    What is the difference between passing to a function by reference and passing by address? They both enable you to modify the variable, so why make two seperate things? Title edit: Passing by reference or address.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,962
    One reason would be that the use of references simplifies the syntax (no need to take the address when passing and no need to dereference the pointer). Another reason would be that while a pointer parameter can be "optional" in the sense that a null pointer can be passed, a reference parameter would not be optional in that way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I think you got confused here. There are only two types of passing parameters: Pass-by-refrence and Pass-by-value. Only pass-by-reference allows you to modify the original variable, whereas pass-by-value will modify only the local copy.
    Maybe you have a more concrete example of what you're trying to achieve?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In machine code they are both the same thing. When using references, you can be sure that the reference itself will be a valid memory address [unless you really jump through hoops to get there, or possibly that you are passing an argument like "*ptr"].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    I think you got confused here. There are only two types of passing parameters: Pass-by-refrence and Pass-by-value. Only pass-by-reference allows you to modify the original variable, whereas pass-by-value will modify only the local copy.
    Maybe you have a more concrete example of what you're trying to achieve?

    QuantumPete
    I took it to be the difference between C style pointer passing and C++ style references.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Passing by reference
    • Doesn't require the address-of (&) operator when passing a variable
    • Doesn't require the dereference (*) operator when changing the value of a reference
    • Guarantees the argument will be an object (you can't have a NULL reference)
    • Guarantees the reference will always refer to the same object


    Pointers are mainly used for memory management, but here's an example to contrast pointers and references:

    Code:
    void f (int& ref)
    {
       ref++; // Pretty
    }
    
    void f (int* ptr)
    {
       (*ptr)++; // Ugly
    }
    
    int main()
    {
       int var = 3;
    
       f (var); // Pass by reference, implicit
       f (&var); // Pass by pointer, explicit
    
       f (NULL); // Oops! Pass by pointer
    
       return 0;
    }
    EDIT: Posted a little too late
    Last edited by rudyman; 09-02-2008 at 09:15 AM.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    In machine code they are both the same thing. When using references, you can be sure that the reference itself will be a valid memory address [unless you really jump through hoops to get there, or possibly that you are passing an argument like "*ptr"].

    --
    Mats
    Even if you pass *ptr you can't have a NULL reference though, since it would crash as soon as you de-reference ptr; or were you thinking of a different situation?

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    >>I took it to be the difference between C style pointer passing and C++ style references.

    Yes that is what I meant. Thanks for the replies.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Even if you pass *ptr you can't have a NULL reference though, since it would crash as soon as you de-reference ptr; or were you thinking of a different situation?
    I'm not sure, but if we do:
    Code:
    void foo(int &x)
    {
        x++;
    }
    
    int main()
    {
       int *px = 0;
       foo(*px);
    }
    I believe this will pass the address in px (0) to foo, and foo would crash. And you would have to do:
    Code:
    void foo(int &x)
    {
       if (!&x)
          return; 
       x++;
    }
    to avoid that.

    [I have just compiled the above (first version) code, and it does indeed crash inside the function foo when compiled with g++ mingw 3.4, whilst second version doesn't crash].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This dereferences a null pointer, so the behaviour is undefined even before it gets into the function.

    Passing a reference signals that no checks are necessary (the wrong if at all has already been done). When a pointer is passed, it should probably always be checked for NULL, unless perhaps it is known to always come from a trusted source.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    All I know is that many years ago, during a code review, I suggested that the dev team check if a NULL reference was passed in... Well, they made it a point to have a good laugh by bringing that up as often as possible.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    IMO, the thing is that undefined behaviour has already happened before that let you create a NULL reference in the first place, so the test would be too late. You won't test if an array index is out of bounds after you have accessed it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anon View Post
    IMO, the thing is that undefined behaviour has already happened before that let you create a NULL reference in the first place, so the test would be too late. You won't test if an array index is out of bounds after you have accessed it.
    Exactly.
    I think I found that it was crashing inside the function too, rather than on the *ptr line, so that's why I thought it was actually creating a NULL reference; but it was just a symptom of undefined behavior.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    On the language standard level, the UB occurs at the moment you do *ptr. At the machine code level of all implementations I know, however, "int& r = *ptr" is a simple copy of the address, so only using the reference will trigger the machine exception.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree with CornedBee - I was just trying to point out that a reference can lead to access to incorrect memory, and yes, the fault is in the calling code that doesn't check the pointer before making a reference of it. Unfortunately, that doesn't necessarily help in the debugging when you are several levels of complicated calls down from where the reference was set.

    And no, I'm not suggesting that NULL checks are made to references - I just wanted to prove to myself that they can be checked. Checking for NULL on anything that isn't a pointer in the first place is meaningless - consider this:
    Code:
    struct A
    {
       int x;
       int y;
    };
    
    A *pa = 0;
    
    extern foo(int &x);   // Refers to the above foo with NULL check. 
    
    int main()
    {
        foo(pa->y);
    ...
    }
    Address 4 in memory will be the result of this, and it will crash just as much as address zero.

    Out of bounds memory addresses can be achieved all sorts of different ways, and not only with NULL, so checking for NULL is not really helping - just let it crash if it's not right, and debug the exception. However, make sure that whenever possible, you check the range when indexing and return values from memory allocations!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 09:48 AM
  2. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 12:25 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21