Pass by reference vs pass by pointer

This is a discussion on Pass by reference vs pass by pointer within the C++ Programming forums, part of the General Programming Boards category; What is the difference between these two alternatives? Are there performance tradeoffs? Any help appreciated. Examples below. Code: //Pass by ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    11

    Pass by reference vs pass by pointer

    What is the difference between these two alternatives? Are there performance tradeoffs? Any help appreciated. Examples below.

    Code:
    //Pass by pointer
    
    void negate(int * pn)
    {
     *n=-*n;
    }
    
    void func()
    {
     int m=10;
     negate(&m);//pass m's address<
     std::cout<<m<<std::endl; //display: -10
    }
    
    
    
    //Pass by reference
    void negate(int & n)
    {
     >n=-n;//modifies caller's argument
    }
    void func()
    {
     int m=10;
     negate(m);//pass by reference
     std::cout<<m<<std::endl; //diplay: -10
    }

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Both should work the same, in that the function can modify the value, their speed should also be the same due to compiler optimization.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    A pointer can be NULL, a reference cannot.

    Pointers sometimes have tricky precedence which a reference hides from you.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    If I am passing a large vector structure that contains classes that I created, then which way of passing parameters would I use? I am guessing it doesn't matter. Would accessing elements of the vector be faster using one way as opposed to another?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    It shouldn't be faster to access the vector one way or the other.

    Normally I would pass by reference unless there is a reason to allow for a null value which is probably not the case here. Just make sure it is obvious that the function modifies the argument by using good function and variable names and comments.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > If I am passing a large vector structure that contains classes that I created
    Why not try it, and put a trace statement in your copy constructor to see how much it gets hammered when you use pass by value.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    Well I already know that pass by value isn't good except for when I am passing small variables around that I don't want the called function to change. I am only curious of the performance difference between pass by reference and pass by pointer (eg address).

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > I am only curious of the performance difference between pass by reference and pass by pointer (eg address).
    You mean like "none at all" ?
    They both share the same underlying implementation as far as most compilers are concerned.

    Another advantage of references over pointers is that references are type-safe.

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    >Why not try it, and put a trace statement in your copy constructor to see how much it gets hammered when you use pass by value.

    Salem,
    Is a trace statement just where you make it print out a variable basically? Never heard the term. (yes I am still a noob in MANY ways )

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    11
    But references can only have one alias right? Meaning after they become referenced to another object they cannot be referenced to another object. I think pointers don't have this limitation. Let me know if I am totally wrong or only partially correct. Thanks

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    This is correct, but for a function parameter it doesn't really matter. Once you have passed an object to the function, you rarely need to change what the parameter refers to. In fact, this is generally an advantage of the reference in this case, since you don't have to worry about that. You can assume that it refers to a non-null object and that it won't change for the duration of the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference to a pointer to a pointer
    By Sharke in forum C++ Programming
    Replies: 18
    Last Post: 05-26-2009, 03:47 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 02:45 PM
  4. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

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