By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

C++: pointers Vs references

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Web Developer <no****@hotmail.com> wrote in message
news:3f**********@news.iprimus.com.au...
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

Jul 19 '05 #2

P: n/a

"David White" <no@email.provided> wrote in message
news:ta****************@nasal.pacific.net.au...
Web Developer <no****@hotmail.com> wrote in message
news:3f**********@news.iprimus.com.au...
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
Jul 19 '05 #3

P: n/a
>
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
Jul 19 '05 #4

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh************@ID-196037.news.uni-berlin.de...

"David White" <no@email.provided> wrote in message
news:ta****************@nasal.pacific.net.au...
Web Developer <no****@hotmail.com> wrote in message
news:3f**********@news.iprimus.com.au...
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.
Jul 19 '05 #5

P: n/a
"Josephine Schafer" <js*@usa.net> wrote in message
news:bh************@ID-192448.news.uni-berlin.de...
"David White" <no@email.provided> wrote in message
news:ta****************@nasal.pacific.net.au...
A pointer can be null; a reference cannot.
Also there is no such thing as a null reference.


You must have missed it :)

DW

Jul 19 '05 #6

P: n/a
> > 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
Jul 19 '05 #7

P: n/a
> 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

Jul 19 '05 #8

P: n/a
ghl
"Web Developer" <no****@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...
> > 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

Jul 19 '05 #9

P: n/a
> 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
Jul 19 '05 #10

P: n/a
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.

Jul 19 '05 #11

P: n/a
On Mon, 11 Aug 2003 15:53:46 +0930, "Web Developer" <no****@hotmail.com>
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."

Jul 19 '05 #12

P: n/a
ghl
"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bh************@ID-133164.news.uni-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
Jul 19 '05 #13

P: n/a
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

Jul 19 '05 #14

P: n/a
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.

op*****@yahoo.com (Samuel Barber) wrote in message news:<37**************************@posting.google. com>...
Dave Rahardja <as*@me.com> wrote in message news:<v4********************************@4ax.com>. ..
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

Jul 19 '05 #15

P: n/a
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
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.