Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++: pointers Vs references

Reply
Thread Tools

C++: pointers Vs references

 
 
Web Developer
Guest
Posts: n/a
 
      08-11-2003
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


 
Reply With Quote
 
 
 
 
David White
Guest
Posts: n/a
 
      08-11-2003
Web Developer <(E-Mail Removed)> wrote in message
news:3f37365f$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-11-2003

"David White" <(E-Mail Removed)> wrote in message
news:taHZa.656$(E-Mail Removed)...
> Web Developer <(E-Mail Removed)> wrote in message
> news:3f37365f$(E-Mail Removed)...
> > 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      08-11-2003
>
> 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


 
Reply With Quote
 
Josephine Schafer
Guest
Posts: n/a
 
      08-11-2003

"John Harrison" <(E-Mail Removed)> wrote in message
news:bh7fg8$uvbb1$(E-Mail Removed)-berlin.de...
>
> "David White" <(E-Mail Removed)> wrote in message
> news:taHZa.656$(E-Mail Removed)...
> > Web Developer <(E-Mail Removed)> wrote in message
> > news:3f37365f$(E-Mail Removed)...
> > > 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.


 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      08-11-2003
"Josephine Schafer" <(E-Mail Removed)> wrote in message
news:bh7i74$eh5cp$(E-Mail Removed)-berlin.de...
>
> > "David White" <(E-Mail Removed)> wrote in message
> > news:taHZa.656$(E-Mail Removed)...


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


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


You must have missed it

DW



 
Reply With Quote
 
Web Developer
Guest
Posts: n/a
 
      08-11-2003

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


 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      08-11-2003
> 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



 
Reply With Quote
 
ghl
Guest
Posts: n/a
 
      08-11-2003
"Web Developer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> > > > > 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



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-11-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
how to understand references to variables and references to constants are distinguished? baumann.Pan@gmail.com C++ 3 11-10-2004 04:16 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments