difference between pointers and references

This is a discussion on difference between pointers and references within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight What is the "whole concept of a reference"? References in Java are indeed reference types in ...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    What is the "whole concept of a reference"? References in Java are indeed reference types in that a reference variable refers to some object. Yet, they are different and in a way more limited than references in C++, yet in a way more powerful than references in C++. They behave like pointers, yet they lack pointer arithmetic and do not need to be dereferenced like a pointer. Does the new keyword in Java return a pointer? Conceptually, I'd say no, yet in a way it does, insofar as references in Java are pointers without pointer syntax.
    Great, now I have a headache from all that bouncing around!
    "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

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    5,879
    The basic concept of a reference in C++ is that it is an alternate name for an object. It is therefore not possible to have a null reference, as having an alternate name for something non-existant is meaningless. All the names refer to nothing.

    One consequence of this concept, which allows plenty of optimisations by the compiler, is that a reference is assumed to refer to something that exists, IF it is used. For example, the compiler is not required to add runtime checks to ensure a reference is valid - it is assumed to be valid, so doesn't need to be checked. That is the case because any act that creates an invalid reference (eg a null reference, a dangling reference) is specified to have undefined behaviour. If the act of creating something is undefined, the result of using it is also undefined.

    A pointer, on the other hand, is something that optionally contains the address of an object. This means a pointer may be NULL, indicating it points at no object. Dereferencing such a pointer is, in itself, undefined behaviour (and, since creating a null reference requires dereferencing a null pointer, that explains why creating a null reference gives undefined behaviour - let alone using that null reference). The difference is that the programmer can check if a pointer is null before dereferencing it, and hence avoid that form of undefined behaviour.

    Because of this, a C++ reference is sometimes described as a glorified pointer. It is not. At most, it is a deliberately constrained pointer. The constraints can't eliminate programmer error (there is no upper bound on programmer stupidity, and there is always opportunity for a programmer to deliberately cause error) but they can help reduce programmer error.

    The concepts of a reference in other languages are a bit different.

    As laserlight noted, Java's concept of a reference has some attributes that, in C++, are only associated with pointers. In effect, it might be said, that the Java designers sought to publically eliminate pointers as an "unsafe construct" but then found they had to add to quietly implement some of those unsafe features into their reference type. A Java reference therefore allows more dangerous practices (i.e. opportunities for programmer error) than does a C++ reference. That's not an indictment of Java - it's simply a realistic result in programming language design when a marketing imperative ("we are creating a safer C++!!!!") intersects with pragmatics ("if something is too safe it can be unusable").
    Right 98% of the time, and don't care about the other 3%.

  3. #18
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Had to read your post a couple times in order to have it sink in grumpy.

    It's a very interesting conundrum to say the least, that languages who are so adamant about eliminating pointers, have to end up implementing them in the first place in order for the language to even work.

    I guess I'll never understand marketing...

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

Similar Threads

  1. Passing Pointers or References?
    By leeor_net in forum C++ Programming
    Replies: 24
    Last Post: 02-04-2009, 01:29 PM
  2. Pointers and references?!? Help!
    By alyeska in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2008, 10:23 AM
  3. Pointers v References
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 06-30-2008, 01:29 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 03:21 PM

Tags for this Thread


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