Pointer vs Reference

J

James Kanze

All references are pointers, but none are the kind of pointers usually
meant when discussing C or C++. For example in C and C++ if you have a
pointer p and do "p++;" that is an operation on the pointer. Whereas in
Java, if you have a reference r and do "r++;" that would be an operation
on the object r refers to [...]
So can we think of a C++ reference as:
Same as a pointer, 4 bytes.

No. The standard is very explicit; a reference doesn't have to
occupy any memory at all. (And the size of a pointer isn't
necessarily 4 bytes. For that matter, it isn't necessarily the
same for all pointer types.)

Since a reference isn't an object, and you can never obtain its
address, it's really irrelevant.
Same as a pointer, points to some where.

It designates an object. How it does so is up to the
implementation.
Main difference: with pointer, you can print out the pointer,
set the pointer to different values, including 0. With
reference, you can't. You always dereference a reference when
you use it.

That's a frequent way of thinking about it, and it may help
understanding. But formally, a reference is simply an lvalue
which refers to an object.
Here are some C++ and C code equivalent:
int a = 10; // In C: the same
int avg = 20; // In C: the same
int& b = a; // In C: int *pa = &a;
printf "%d", b; // In C: printf "%d", *pa;
b = 20; // In C: *pa = 20;
// can't do // In C: pa = (int *) 0; or (int *) NULL;
// can't do // In C: pa = &avg;
int& c = b; // In C: int *pa2 = &(*pa)
// &(*pa) is &(20) which is illegal
// but if it is C++, it magically
// changes &(*pa) to just pa
// So in C, int *pa2 = pa;
So In Java, Python, PHP5, and Ruby, when you use
a = Dog.new
it is really not a reference, not a pointer, but
something in between.

In C++ terminology, it's a pointer. The only difference is that
in Java (and I presume, the other languages as well), pointers
aren't a recognized object type, so you cannot to things with
them that you could do with an object type. (In Java, at least,
this is also the case for the basic types, such as int or
double.)
Not a reference, because you can set where it points to:
Not a pointer, because you use

instead of a->bark() or (*a).bark()

That's just syntax.
 
J

James Kanze

In Java I believe the official definition of pointer is "a non-null
reference". However, nearly every Java programmer uses the term to
mean a low level machine pointer like C++ uses with manual referencing
and almost no restrictions on what you can do with it -- e.g. assign
it numeric values, do arithmetic, compose it with bit operators etc.

That's the terminology in a specific community. Each community
developes its own terminology. (I've heard people say that Java
references aren't pointers because Java doesn't support pointer
arithmetic. But of course, saying that the difference between
a pointer and a reference is that pointers support arithmetic is
pretty arbitrary.)
References are pointers with airbags.

References in Java, you mean:). (But I like the expression.)

In C++, references and pointers are two very different things.
So the C++ community gives them different names, and insists on
the difference, the fact that the names name different things.
In Java (and most other languages), you don't really have two
separate concepts here, so in a very real sense, references and
pointers can be considered two different names for more or less
the same thing. Distinguishing the two doesn't necessarily add
any clarity or help the understanding of Java in any real way,
where as it's essential for C++. If I had to characterize
Java's references in terms of C++, I'd say that they were
pointers, but then point out that 1) pointers (like int and
double) aren't "objects" in Java: a pointer doesn't have an
address, you can't new it, etc., and 2) Java doesn't support
pointer arithmetic (just as it doesn't support implicit
conversion of double to int).
 
K

Kai-Uwe Bux

James said:
Alf really got it right when he said that it's just arbitrary
terminology, and that each language has its own definitions.
Never the less: the difference between pointers (called
references) in Java, and pointers in C++, is that in C++, a
pointer is, itself, an object in its own right. Unlike Java,
C++ doesn't divide its types into two incompatible categories;
an object can have any type you want, any type may support copy,
operators, etc., and you can take the address of any type.

Just a nit: I think you should say that C++ does not devide its _objects_
into incompatible categories. The type system, however, does not look at
all that homogeneous. It strikes me that there are at least three different
categories of types:

a) function signatures
b) object types (built-in types, pointers, classes, enums, maybe more)
c) reference types

It is true that you cannot have objects of types in the classes a) and c).
However, the template mechanism recognizes those as distinct types of their
own, e.g., the type

bool(int)

is different from

bool(*)(int)
And
pointers are just another object type, with no particular
special rules. All of the rest (pointers to pointers, etc.)
follows.


Best

Kai-Uwe Bux
 
S

Stefan Ram

James Kanze said:
in C++, a pointer is, itself, an object in its own right.

In C++,

»An object is a region of storage. (...)«

ISO/IEC 14882:2003(E), 1.8

A pointer is a value of a pointer type, like »&c« or »( int * )0«.

A pointer, therefore, does not need to be an object.
 
G

Guest

In C++,

»An object is a region of storage. (...)«

ISO/IEC 14882:2003(E), 1.8

A pointer is a value of a pointer type, like »&c« or »( int * )0«.

A pointer, therefore, does not need to be an object.

A pointer also have an address (i.e. occupy some region of storage).
 
P

Pete Becker

Never the less: the difference between pointers (called
references) in Java,

Almost always called references. When you dereference a null reference
in Java, the runtime throws a NullPointerException object. <g>
 
M

Mike Schilling

Pete said:
Almost always called references. When you dereference a null reference
in Java, the runtime throws a NullPointerException object. <g>

For fun, I grepped the JLS for "pointer". Almost all of the hits are the
word NullPointerException, though there's also the definition of "reference"

The reference values (often just references) are pointers to these
objects,
and a special null reference, which refers to no object.

and a few slipups where "reference" was meant.
 
O

Old Wolf

Until when C++ comes along, then we have a new "reference":

int a = 10;
int i =& a; // or int i = &a; i am not sure about the syntax.
i = 20; // now both a and i are 20
so this type of reference is an implicit pointer... it points to a,
but
you don't use the way in C (int *pi = &a) And when you use (i = 20),
it does the dereference silently. (*pi = 20;)

It is confusing to think of them this way. I like
to call them "aliases" to avoid the confusion
with the computer science term "reference" , which
can be applied to pointers.

Imagine a cubby-hole in your computer's memory
with the value 20 in it. Above the hole are the
labels "a" and "i".

The labels "a" and "i" both equally refer to the
same memory location. These two code snippets
have exactly identical effects:

int i = 20;
int &a = i;

and

int a = 20;
int &i = a;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top