C++: pointers Vs references

Discussion in 'C++' started by Web Developer, Aug 11, 2003.

  1. Hi,

    Can someone provide a short and concise statement(s) on the difference
    between pointers and references. A graphical representation (via links?) of
    both would be much appreciated as well.


    WD



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
    Web Developer, Aug 11, 2003
    #1
    1. Advertising

  2. Web Developer

    David White Guest

    Re: pointers Vs references

    Web Developer <> wrote in message
    news:3f37365f$...
    > Hi,
    >
    > Can someone provide a short and concise statement(s) on the difference
    > between pointers and references.


    A pointer is an object containing the address in memory of another object; a
    reference is an alias for another object.
    A pointer can be null; a reference cannot.
    A pointer can be re-seated (i.e., pointed at a different object); a
    reference cannot.
    A reference must be initialized (i.e., its refer-ee specified) when it is
    created; a pointer can be uninitialized.

    > A graphical representation (via links?) of
    > both would be much appreciated as well.


    I'm not sure what one would look like. Anyway, I am unable to provide one.

    DW
    David White, Aug 11, 2003
    #2
    1. Advertising

  3. Re: pointers Vs references

    "David White" <> wrote in message
    news:taHZa.656$...
    > Web Developer <> wrote in message
    > news:3f37365f$...
    > > Hi,
    > >
    > > Can someone provide a short and concise statement(s) on the difference
    > > between pointers and references.

    >
    > A pointer is an object containing the address in memory of another object;

    a
    > reference is an alias for another object.
    > A pointer can be null; a reference cannot.
    > A pointer can be re-seated (i.e., pointed at a different object); a
    > reference cannot.
    > A reference must be initialized (i.e., its refer-ee specified) when it is
    > created; a pointer can be uninitialized.
    >


    Also a pointer is syntactically more convenient, how would you write an
    assignment operator or copy constructor without using references?

    john
    John Harrison, Aug 11, 2003
    #3
  4. Re: pointers Vs references

    >
    > Also a pointer is syntactically more convenient, how would you write an
    > assignment operator or copy constructor without using references?
    >


    I mean a REFERENCE is syntactically more convenient, of course.

    john
    John Harrison, Aug 11, 2003
    #4
  5. Re: pointers Vs references

    "John Harrison" <> wrote in message
    news:bh7fg8$uvbb1$-berlin.de...
    >
    > "David White" <> wrote in message
    > news:taHZa.656$...
    > > Web Developer <> wrote in message
    > > news:3f37365f$...
    > > > Hi,
    > > >
    > > > Can someone provide a short and concise statement(s) on the difference
    > > > between pointers and references.

    > >
    > > A pointer is an object containing the address in memory of another

    object;
    > a
    > > reference is an alias for another object.
    > > A pointer can be null; a reference cannot.
    > > A pointer can be re-seated (i.e., pointed at a different object); a
    > > reference cannot.
    > > A reference must be initialized (i.e., its refer-ee specified) when it

    is
    > > created; a pointer can be uninitialized.
    > >

    >
    > Also a pointer is syntactically more convenient, how would you write an
    > assignment operator or copy constructor without using references?
    >

    Also there is no such thing as a null reference. This could imply that it is
    more efficient to use references because you need
    not test the validity of a reference before using it.
    Pointers, on the other hand, should generally be tested against null.
    Josephine Schafer, Aug 11, 2003
    #5
  6. Web Developer

    David White Guest

    Re: pointers Vs references

    "Josephine Schafer" <> wrote in message
    news:bh7i74$eh5cp$-berlin.de...
    >
    > > "David White" <> wrote in message
    > > news:taHZa.656$...


    > > > A pointer can be null; a reference cannot.


    > Also there is no such thing as a null reference.


    You must have missed it :)

    DW
    David White, Aug 11, 2003
    #6
  7. Re: pointers Vs references


    > > > > > Hi,
    > > > > >
    > > > > > Can someone provide a short and concise statement(s) on the

    > difference
    > > > > > between pointers and references.
    > > > >
    > > > > A pointer is an object containing the address in memory of another

    > > object;
    > > > a
    > > > > reference is an alias for another object.
    > > > > A pointer can be null; a reference cannot.
    > > > > A pointer can be re-seated (i.e., pointed at a different object); a
    > > > > reference cannot.
    > > > > A reference must be initialized (i.e., its refer-ee specified) when

    it
    > > > > is created; a pointer can be uninitialized.

    >
    > Wait a minute, in Java, an object reference also hold the memory address

    of
    > an object. Can you understand my confusion? I think my background Java may
    > curse me in C++.


    I think I got it now. A reference gets you the value it references, but for
    object references it does not give you the actual value but the memory
    address of where the value lies, and in this latter case, it is EXACTLY the
    same as a pointer.

    Summary: object references in Java are pointers - they are just not
    explicitly defined.

    Note: I realise pointers don't behave like object references in Java.


    Any comments appreciated.


    Regards
    WD



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
    Web Developer, Aug 11, 2003
    #7
  8. Re: pointers Vs references

    > I think I got it now. A reference gets you the value it references,
    but for
    > object references it does not give you the actual value but the memory
    > address of where the value lies, and in this latter case, it is

    EXACTLY the
    > same as a pointer.
    >
    > Summary: object references in Java are pointers - they are just not
    > explicitly defined.


    Java references are closer to C++ pointers than to C++ references. A
    difference between Java references and C++ pointers is that you can
    perform arithmetric operations on C++ pointers e.g.:

    int* a = new int[10];
    int* b = a + 1;
    int* c = b - 1;

    *b = 2; // Modifies a[1]
    *c = 1; // Modifies a[0]

    However typically you want to avoid pointer arithmetric where ever you
    can. If the pointer goes outside allocated memory sooner or later (if
    you are unlucky) nasty things will happen.

    It looks like you are trying to learn C++ in terms of Java. Even though
    the syntax of those languages look deceptively similar, they are really
    different languages with different semantics and idioms. It is better to
    start C++ with a clear and open mind. C++ often requires a different
    approach; and trying to do things the Java way usually doesn't work out
    too well in C++.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 11, 2003
    #8
  9. Web Developer

    ghl Guest

    Re: pointers Vs references

    "Web Developer" <> wrote in message
    news:...
    >
    > > > > > Hi,
    > > > > >
    > > > > > Can someone provide a short and concise statement(s) on the

    > difference
    > > > > > between pointers and references.
    > > > >
    > > > > A pointer is an object containing the address in memory of another

    > > object;
    > > > a
    > > > > reference is an alias for another object.
    > > > > A pointer can be null; a reference cannot.
    > > > > A pointer can be re-seated (i.e., pointed at a different object); a
    > > > > reference cannot.
    > > > > A reference must be initialized (i.e., its refer-ee specified) when

    it
    > > > > is created; a pointer can be uninitialized.

    >
    > Wait a minute, in Java, an object reference also hold the memory address

    of
    > an object. Can you understand my confusion? I think my background Java may
    > curse me in C++.


    It isn't clear what is meant by "the memory address of an object." Since an
    object is a conceptual fiction and consists in reality of various control
    structures, arrays, member data, etc. it is unclear as to just what exactly
    a reference variable points to. Probably most implementation use it to
    address the start of the structure containing all the object information,
    but I don't know that structure myself.

    I also believe that a reference in C++ is not necessarily an address of the
    data object, but could be implemented in some other fashion (say, a handle
    that refers to an address of the data object). The only requirement is that
    the identifier used for the reference can be used interchangeably for the
    declaration name of the object being referenced. Probably most
    implementations simply use the address of the data object. The restrictions
    on the C++ reference prohibit using it like a pointer, which does contain
    the address of the data object pointed to.

    Like a pointer, the Java reference variable contains an address, but the
    language syntax checks the type of the primitive or object being stored into
    it for compatibility. So, a Java reference cannot be used as a pointer; same
    as in C++. In Java a reference variable can be set to null.

    Since I am here to learn C++: can a reference in C++ be set to null? I
    should think not, since it only stands for one object and cannot be changed.

    Yes, these languages are different. It's a pity (in a way) that they use the
    terms "reference" and "object" so differently.
    --
    Gary
    ghl, Aug 11, 2003
    #9
  10. Re: pointers Vs references

    Web Developer wrote:

    >
    > I think I got it now. A reference gets you the value it references, but for
    > object references it does not give you the actual value but the memory
    > address of where the value lies, and in this latter case, it is EXACTLY the
    > same as a pointer.


    No. Using a reference to an object (regardless of whether the object is
    of a built-in or user-defined type) is always exactly the same as using
    the object itself. All a reference does is give you a different name for
    the same object.

    >
    > Summary: object references in Java are pointers - they are just not
    > explicitly defined.


    Off-topic, and I have no idea.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 11, 2003
    #10
  11. Re: pointers Vs references

    > Since I am here to learn C++: can a reference in C++ be set to null? I
    > should think not, since it only stands for one object and cannot be

    changed.

    You think right; C++ references cannot be 0, if were posible it would be
    useless since C++ references cannot be reseated.

    > Yes, these languages are different. It's a pity (in a way) that they use

    the
    > terms "reference" and "object" so differently.


    I think it is a pitty Java plagiarises the C++ syntax while at the same time
    having different semantics. This leads to unnecessary confusion for people
    moving from C++ to Java and visa versa.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 11, 2003
    #11
  12. On Mon, 11 Aug 2003 15:53:46 +0930, "Web Developer" <>
    wrote:

    >Hi,
    >
    >Can someone provide a short and concise statement(s) on the difference
    >between pointers and references. A graphical representation (via links?) of
    >both would be much appreciated as well.


    Reading your other posts, I assume you are a Java programmer.

    References are what Java programmers are used to. They "refer" to some object,
    and are "bound" to an object during creation, and are used as aliases to that
    object. Because references are aliases, the "contents" of a reference is
    actually the contents of the object they are bound to.

    Pointers "point" to an object. They are not aliases of the object it points
    to. The "contents" of a pointer is the _address_ of an object.

    Pointers are not "bound" to any object; rather, they simply "point" to an
    object. The value of a pointer can be changed during its lifetime, causing it
    to "point" to different objects at different times. References are "bound" to
    an object, and it cannot be rebound during its lifetime.

    And I can almost anticipate your next question; and the answer is: "use
    references when you can, use pointers when you must."
    Dave Rahardja, Aug 12, 2003
    #12
  13. Web Developer

    ghl Guest

    Re: pointers Vs references

    "Peter van Merkerk" <> wrote in message
    news:bh8vai$vbhjl$-berlin.de...
    > I think it is a pitty Java plagiarises the C++ syntax while at the same

    time
    > having different semantics. This leads to unnecessary confusion for people
    > moving from C++ to Java and visa versa.


    Indeed. Much confusion. But "plagiarises" seems too harsh or, maybe, legal a
    term.
    Who exactly does own the syntax of various languages?
    Could I copyright the + operator and sue everybody in sight? (Probably not;
    it's too old in usage.)

    Still, the main point is taken and I think correct. If you want to define a
    new language -- make up an entirely new syntax!
    --
    Gary
    ghl, Aug 12, 2003
    #13
  14. Re: pointers Vs references


    > > I think it is a pitty Java plagiarises the C++ syntax while at the

    same
    > time
    > > having different semantics. This leads to unnecessary confusion for

    people
    > > moving from C++ to Java and visa versa.

    >
    > Indeed. Much confusion. But "plagiarises" seems too harsh or, maybe,

    legal a
    > term.


    I'm not a native english speaker, so sometimes it is difficult for me to
    assess if something "sounds" too harsh. Maybe "copied" is more
    appropriate word in this context.

    > Who exactly does own the syntax of various languages?


    The C++ Standard document itself is copyrighted by ITI, ISO, IEC and
    ANSI. I'm not sure if that copyright also applies to the C++ syntax
    itself; I'm not a lawyer.

    > Could I copyright the + operator and sue everybody in sight? (Probably

    not;
    > it's too old in usage.)


    Because of prior art I'm inclined to believe it is not possible. If it
    were possible you'd be rich in no time; the + operator is used
    everywere. However, even though I wish you the best, I hope that such a
    patent is impossible, as it could only be used to frustrate rather than
    stimulate new developments.

    > Still, the main point is taken and I think correct. If you want to

    define a
    > new language -- make up an entirely new syntax!


    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Aug 12, 2003
    #14
  15. Web Developer

    Jason Thomas Guest

    He's absolutely right. Java "references" are actually called
    references, but they are more similar to C/C++ style pointers. In fact
    they can be pointed to NULL (null in Java).

    Java programmers are more used to dealing with pointers than
    references. The "new" operator in C++ returns a pointer to some memory
    in which that object, structure, union, enum, or primitive type is
    stored:

    I.E:

    Object* myObj = new Object();

    Seems vaguely similar to the Java syntax:

    Object myObj = new Object();

    A reference is instead a handle directly to some memory that can't be
    reassigned, and is automaticly dereferenced. It's more like an alias
    to an already existing variable.

    Object myObj; //Allocates an object on the stack.
    Object& myObjRef = myObj; //Makes an alias to myObj called "myObjRef."

    myObj.doSomething();
    myObjRef.doSomething(); //Does exactly the same thing as above.

    Another example would be:

    int myInt = 5;
    int& myIntRef = myInt; // Alias to myInt;

    myInt = 6;
    printf("%d\n", myIntRef); // Prints out 6.


    The basic difference is, if you use a pointer you actually are just
    using a handle to an object. You can reassign that handle at any time
    to some other Object. It's basically just a variable that holds a
    memory address and is associated with a type.

    This is really a big deal with a function:

    int bob(Object* myPtr, Object& myRef)
    {
    myPtr = NULL; //The object passed isn't effected.
    myRef = NULL; //The actual object passed is changed, not just in
    this scope.
    }

    I personaly believe references are superfluous, and hide the fact that
    an arguement you've passed may be altered in the function you're
    passing to. I'd limit my use of them if I were you. Pointers are a
    great deal more robust, easy to use and understand, and it's clear
    what is going on; nothing is kept secret from the programmer.

    -Jason Thomas.

    (Samuel Barber) wrote in message news:<>...
    > Dave Rahardja <> wrote in message news:<>...
    > > Reading your other posts, I assume you are a Java programmer.
    > >
    > > References are what Java programmers are used to.

    >
    > No. Java does not have C++ style references. Java has implicit
    > pointers. They differ from C/C++ pointers in that you can't do pointer
    > arithmatic, or unsafe casts.
    >
    > Sam
    Jason Thomas, Aug 16, 2003
    #15
  16. Web Developer

    Ghana Guest


    > I personaly believe references are superfluous, and hide the fact that
    > an arguement you've passed may be altered in the function you're
    > passing to. I'd limit my use of them if I were you. Pointers are a
    > great deal more robust, easy to use and understand, and it's clear
    > what is going on; nothing is kept secret from the programmer.
    >
    > -Jason Thomas.


    Well Java is a high abstraction programming language. That is, you don't
    really need to know the underlying details of how it works in order to use
    it. I feel C++ is becomming more and more like Java as it adds more levels
    of abstraction (ie the recent introduction of the String data type).


    -dsf


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
    Ghana, Aug 18, 2003
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    434
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    447
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    710
    Bruno Desthuilliers
    Dec 12, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    668
Loading...

Share This Page