Thread: Pointers/references

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    Question Pointers/references

    Hi, I was wondering what the difference between a pointer and a reference is. I generally references when passing classes or something with a structure.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Google "pointers vs references".

    It's also clear that you don't have a C++ book, which is a condition you should remedy.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    From the code generated perspective: Not a lot [except when the compiler can completely remove a reference and just access the original variable, whilst a pointer would have to go through the pointer at all times]. From the usage perspective, quite a bit. Aside from syntactical differences, references can not be changed after the first assignment (so you can't change WHICH OBJECT a reference refers to), and references are always pointing to a valid object [unless you do really weird undefined stuff].

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

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    rofl I have quite a few C++ books, but none clearly discuss the intricate differences, I know how to use both, I just sort of meant in terms of memory, not necessarily in terms of the language itself.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most compilers (AFAIK) implement references in terms of pointers.
    The standard says a reference may take no memory at all, however.
    The conclusion is: it's implementation-defined. You'll have to look at the assembly output of your compiler to know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    oh, okay, thanks.. but how does something take no memory? does that mean it's only different in the compiler, and once compiled, a reference IS the thing it's a reference to?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by RobotGymnast View Post
    oh, okay, thanks.. but how does something take no memory? does that mean it's only different in the compiler, and once compiled, a reference IS the thing it's a reference to?
    Well generally, that's what a reference means -- it IS the object being referred to. That's how people usually think of them. Whether the compiler inlines it or uses a pointer under the covers depends on a lot of factors and is usually irrelevant to most people.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A reference probably might only exist at code level (but then a pointer in some cases might too?).

    For example, if you have
    Code:
    int i = 5;
    int& ri = i
    ri += 5;
    the compiler might simply translate it to:
    Code:
    int i = 5;
    i += 5;
    and there would be nothing corresponding to the reference.

    This might work for a pointer too, but if I'm not mistaken, it can be harder for the compiler to prove that the pointer is still pointing to the same thing (a reference has to refer to the same thing).

    --------
    Whether you pass things via references or pointers varies among people (although there are some cases where you must use references - e.g operator overloading, copy constructor). Personally I use pointers only where I need the ability to pass NULL, or to change the pointer (e.g pointers to arrays).

    Others prefer to use pointers when the function is intended to modify what is pointed to, as taking the address at the place of call would indicate that.

    Code:
    void foo(int* n)
    {
        *n = 42;
    }
    
    int main()
    {
        int m;
        foo(&m); //clear that something's going on here: m can be modified
    
        //but
        int* p = &m;
        foo(p); //doesn't stand out particularly?
    }
    Last edited by anon; 09-19-2008 at 08:19 AM.
    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).

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by anon View Post
    Others prefer to use pointers when the function is intended to modify what is pointed to, as taking the address at the place of call would indicate that.
    I don't buy that, though.
    If the function isn't going to modify what it takes, it takes a const reference, and otherwise not.
    Nevertheless, the "C++" way is typically to use references wherever possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I don't buy that, though.
    If the function isn't going to modify what it takes, it takes a const reference, and otherwise not.
    Nevertheless, the "C++" way is typically to use references wherever possible.
    Maybe this is just proof that I'm not a C++ programmer to the soul (or is that sole?), but a little while back, I chased a bug where a member-variable was passed to another object, and later on it had changed. That was, because the other object stored a reference to the original variable. That is the kind of "hidden" thing, whilst if it had taken a pointer, I had known IMMEDIATELY that it was potentially modifying the value later on.

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

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have to agree a little more with Elysia on this one. The one time that I try to steer away using references is any circumstance that by design a pointer that could potentially carry a null value is dereferenced.

    Example:
    Code:
    #include <cstdio>
    
    void swap(int &x, int &y)
    {
      int t = x;
      x = y;
      y = t;
    }
    
    int main(void)
    {
      int *l = new int;
      int m = 5;
    
      scanf("&#37;d", l);
      swap(*l, m);
    
      return 0;
    }
    And while true this is not necessarily a likely thing to occur in the real world it illistrates a problem that has many real-world faces in programming.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    A reference probably might only exist at code level (but then a pointer in some cases might too?).

    For example, if you have
    Code:
    int i = 5;
    int& ri = i
    ri += 5;
    the compiler might simply translate it to:
    Code:
    int i = 5;
    i += 5;
    and there would be nothing corresponding to the reference.
    Yes, and you can make that more complex:
    Code:
    int &ri = somestruct.a.b.i;
    could still be done without actually storing the location of the final member i anywhere, since there is little or no benefit [assuming the number of times ri is used is relatively small] to have the extra overhead of storing the location of the final member i vs. using the location directly.

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

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if it had taken a pointer, I had known IMMEDIATELY that it was potentially modifying the value later on.

    This is Stroustrup's argument for using pointers for parameters that can be modified. However (and he says this as well), if you name the function and parameters properly, then it should be just as clear that the argument will change even if you use a reference.

    Because of the other benefits of references, I prefer to use them and just make sure I use clear and meaningful function and parameter names (as well as comments) so that there is no hidden modification of an argument.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I use references when passing variables to functions, but never to store them (too inflexible for that).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 04:21 PM