C++: pointers Vs references

W

Web Developer

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
 
D

David White

Web Developer said:
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
 
J

John Harrison

David White said:
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
 
J

John Harrison

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
 
J

Josephine Schafer

John Harrison said:
object;

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

Web Developer

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
 
P

Peter van Merkerk

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

ghl

Web Developer said:
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.
 
K

Kevin Goodsell

Web said:
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
 
P

Peter van Merkerk

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

Dave Rahardja

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

ghl

Peter van Merkerk said:
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!
 
P

Peter van Merkerk

same

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

Jason Thomas

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

Ghana

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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top