473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C++: pointers Vs references

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

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

"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
"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
> > 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
> 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
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
> 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers...
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
12
by: arganx | last post by:
Before I had a good idea how to use pointers, I was wondering - first, how to use them. And second, of what value would they be. Now that I have at least a pretty good idea of how to use them, I...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
7
by: Erdal Mutlu | last post by:
Hi, I am trying to design a base class (interface) with two or more subclasses as follows: class A { .... virtual static A* getByName(const string x)=0 const; }
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
29
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.