Pointers v References

This is a discussion on Pointers v References within the C++ Programming forums, part of the General Programming Boards category; I must admit that the nuance between these two escapes me. I understand the syntactical differences between using the two, ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    Pointers v References

    I must admit that the nuance between these two escapes me.

    I understand the syntactical differences between using the two, but I do not understand how and when one is more useful than the other.

    Can someone please either explain or point me to a good explanation on this subject? I.e. specifically one contrasting pointers and references, not just separate documentation for pointers and references.

    Thanks!

  2. #2

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    You can think of a reference as basically the same thing as a const pointer, except that it 'implicitly' dereferences itself when you try to write to it or read from it.

    That is, you never have to worry about using dereference or address-of operators with references, so functions can take parameters by reference, and also return by reference, and do it 'behind the scenes'.

    References are usually easier to use for this reason, and because they always point to valid memory and cannot be rebound to another location, they're much safer than pointers.
    Last edited by rudyman; 06-29-2008 at 02:21 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You never have to check a reference for null. That is a big difference. Going the other way, you can't assign null to a reference, so you can't use it for an optional parameter.

  4. #4

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Quote Originally Posted by Daved View Post
    ... you can't assign null to a reference ...
    Well, you can assign null to a reference whose type is 'const [typeof(NULL)]&', which would essentially create an alias for NULL. But yes, a reference is guaranteed to refer to a valid object, and it is implicitly const and therefore cannot be rebound like pointers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by rudyman View Post
    'const [typeof(NULL)]&'
    What the holy hell is that???

  6. #6

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    My compiler says that typeof NULL is const int, so it would be valid to say 'const int& null_ref = NULL;', although that isn't portable code, yet.
    Last edited by rudyman; 06-29-2008 at 03:58 PM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    NULL in C++ is defined as 0.
    const int& null_ref = NULL is valid and portable, but it will create a temporary int to hold 0.
    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.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    And why would one want to do that anyways...

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can think of a pointer as a variable holding a memory address, and a reference as an alias name for an object. The implementation of the two may be the same, but the implementation is irrelevant when it comes to thinking about them.

    Thus, a pointer is used when you deal with memory - dynamically allocated objects (new and delete) and iteration over an array come to mind.
    You use a reference when you deal with aliases, mostly in parameter passing if you don't want to copy the argument.
    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

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by rudyman View Post
    My compiler says that typeof NULL is const int, so it would be valid to say 'const int& null_ref = NULL;', although that isn't portable code, yet.
    That is in actual fact, uncompileable rubbish.
    There is no such thing as a null-reference, the standard says so!

    Things with references that differ from pointers:
    1. A reference cannot be null
    2. A reference must be assigned upon creation
    3. A reference cannot be changed to refer to something else
    4. A const reference is the only thing that can extend the lifetime of a temporary object
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    That is in actual fact, uncompileable rubbish.
    There is no such thing as a null-reference, the standard says so!
    Actually, it is not the same as a "null reference". It is merely a const reference to an int that is bound to an integer literal. Since const references may be bound to rvalues, this is legal (and thus compilable), as Elysia has pointed out. It is still rubbish in the sense that NULL is intended as a null pointer constant, so initialising an int reference with it is not semantically correct.
    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

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It just doesn't mean what rudyman thinks it means.
    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

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    But you can emulate a reference!
    Thus it's possible to create a "null reference"
    Well, bad idea anyway. Most will disagree with such approaches.
    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.

  14. #14

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Quote Originally Posted by CornedBee View Post
    It just doesn't mean what rudyman thinks it means.
    If null is defined as 0, and you can create a reference to 0, then you can create a reference to null. I somehow managed to say that in the most poorly worded way possible, but I was just pointing out that although technically there's no reason or use for it, it is possible to assign and check a reference to be null.
    Last edited by rudyman; 06-30-2008 at 09:51 AM.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    If null is defined as 0, and you can create a reference to 0, then you can create a reference to null. I somehow managed to say that in the most poorly worded way possible, but I was just pointing out that although technically there's no reason or use for it, it is possible to assign and check a reference to be null.
    I think that your wording is fine, but you should have clarified Daved's wording outright instead of trying to be funny by taking it literally. Clearly, Daved meant that while there are null pointers, there are no null references since references always refer to objects (though due to a programming error they might "refer" to an object that no longer exists, but this would be in the realm of undefined behaviour, if I remember correctly).
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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