Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Diff between Pointer and ref

1 view
Skip to first unread message

Anil - Sehgal

unread,
Sep 1, 1998, 3:00:00 AM9/1/98
to

We can pass an object to a function as a pointer or as a reference.

Can anybody explain about what are the relative advantages / disadvantages
of using references over pointers. Internally references are also passed
as address in some way or the other.

Thanks,

ANil Sehgal


Jon Bell

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to

If you want to "re-seat" the pointer, as may happen when you are
manipulating linked lists, you need to use a pointer. References cannot
be "re-seated."

Otherwise, if you are passing via pointer/reference only for efficiency
(to avoid copying the object) or because you want to modify the object
inside the function:

When you pass an argument to a function using a reference, you don't have
to remember to put an '&' on the argument in the function call, nor do you
have to remember to de-reference it in the body of the function. Some
people consider this to be an advantage. Other people consider it to be a
disadvantage because they can't easily see whether the argument is likely
to be modified.

When you pass an argument to a function using a pointer, the opposite
arguments apply.

It seems to me that preferring pointers or references is influenced
largely by previous programming experience. Former C programmers tend to
prefer pointers, because that's what they're accustomed to using in C.
Former Pascal or Fortran programmers (like me) tend to prefer references,
because it more closely mimics the syntax that they are used to.

--
Jon Bell <jtb...@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

Joseph K. Barrett

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
Thank you for a singularly clear answer about that aspect of the difference
between the two different methods. It does not say it all however, it must be
thought carefully about on a case by case basis for writing really good
(maintainable) programs. Further the chioce should be documanted in the source
(a la literate programming).

Joseph

Stan Lippman

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
*** hmmm. i was hoping you'd make explicit the aspects to look for in each
case. one difference between the use of a pointer and reference is that
in the use of a pointer the function parameter is saying that an actual
argument
of a nil pointer is something that may correctly occur:

extern void foo( XX *pf );
foo( 0 ); // ok

-- if it is meaningless in the program, the solution is left to the
programmer:
the function must test whether pf is 0 and behave accordingly.

when a parameter is made a reference, then there is no possiblity of a
reference to no object being passed. thus, the function is saying that it is

not meaningful to pass no object to the function, which is the case with a
pointer argument.

when we declare

extern void foo( const XX &rf );

the call

foo( 0 );

only compiles if there is a conversion from the integer constant 0 to an
object
of type XX. a temporary is generated, and rf is aliased to that.

is this the sort of thing you mean by case by case? in general, when you
make a
posting in which you assert something, it is helpful to readers of the news
group
if you clarify your point with a brief example. an ideal posting, in this
case, would
provide at least two cases: one in which a pointer is the clear choice, and
one in
which the reference is the clear choice.

so. there you go :-)

stan lippman

Joseph K. Barrett wrote:

--

``If you can put your five fingers through it, it is a gate, if not, a door.
Shut your eyes and see.'' (J. Joyce)
------------------------------------------------------------------
Stanley Lippman, Objectwrite, Inc.
310-785-0363(v/f), 310-772-0561(home)
1501 South Reeves Street, Los Angeles, CA 90035
------------------------------------------------------------------
slip...@objectwrite.com http://www.objectwrite.com
(or, when the url & mail forwarding breaks) ::
stan...@mediaone.net http://people.we.mediaone.net/stanlipp
------------------------------------------------------------------
Training and Consulting in C++ and Object-Oriented
Programming, Performance, and Design
------------------------------------------------------------------

Joseph K. Barrett

unread,
Sep 2, 1998, 3:00:00 AM9/2/98
to
Much of the behavior is ihnerited from C and a function returning a NULL pointer failed
to allocate memory for the object. PJ, Mr Tobler and/or Bjarne help me out here. I
thought I was not in over my head, but I may have erred. Stan I wish that I actually
competent enough to really answer you but I am respectful of my limitations.

Joseph

Jon Bell

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to
Stan Lippman <stan...@mediaone.net> wrote:
>*** hmmm. i was hoping you'd make explicit the aspects to look for in each
> case. one difference between the use of a pointer and reference is that
> in the use of a pointer the function parameter is saying that an actual
> argument of a nil pointer is something that may correctly occur:

Yes, that's a good point, in general. I was thinking (and assuming that
the original questioner was thinking) of the situation in which the object
to be passed already exists, and the question was how to pass it to the
called function. If the called function may be used in a situation where
the calling function has only a pointer to begin with, then the
called function should check whether it's nil. With a reference, one
doesn't have to worry about this.

My excuse for not mentioning this is that pointers, dynamic memory
allocation and linked data structures are "advanced" topics in the
introductory courses that I teach, so those situations aren't normally
near the top of my head at the beginning of the first semester. :-)

ethu...@my-dejanews.com

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to
In article <Pine.GSO.3.96.980901224647.26831D-100000@dilbert>,

Anil - Sehgal <ase...@cs.tamu.edu> wrote:
>
> We can pass an object to a function as a pointer or as a reference.
>
> Can anybody explain about what are the relative advantages / disadvantages
> of using references over pointers. Internally references are also passed
> as address in some way or the other.
>
> Thanks,
>
> ANil Sehgal
>
>

Anil,
I have some idea of the difference. A pointer is always going to be an address
to a memory location, a pointer is a pointer is a pointer.

How a reference is implemented depends on the compiler. I think many, or
perhaps most compiler makers implement references as handles. Now, 'What's a
handle...' This is also dependent on implementation, but it is usually a
double- indirection pointer, a pointer to a pointer to the 'stuff' you are
interested in. The advantage of this double indirection is that an object in
memory may have many other objects referring to it. If this object changes
its location in memory for any reason, with a handle, it only needs to update
one pointer in a list of pointers that the other objects are referring to so
that all references remain valid, rather than having some method of tracking
down all objects that refer to it.

As for the advantages of one over the other, there is probably plenty of
debate on that. I would guess that in fairly simple situations when you know
how the objects are going to be used and stored, that pointers are fine. The
reference, being 'self dereferencing' can be more convenient and allow for
cleaner code, and, I suppose, for complex situations involving many objects
referencing each other, would be preferred.

I'm a little fuzzy on the details, but I think this is the gist of it.

Eric Thurston

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Jon Bell

unread,
Sep 3, 1998, 3:00:00 AM9/3/98
to
<ethu...@my-dejanews.com> wrote:
>In article <Pine.GSO.3.96.980901224647.26831D-100000@dilbert>,

>
>How a reference is implemented depends on the compiler. I think many, or
>perhaps most compiler makers implement references as handles. Now, 'What's a
>handle...' This is also dependent on implementation, but it is usually a
>double- indirection pointer, a pointer to a pointer to the 'stuff' you are
>interested in.

Can you give a specific example of a compiler that does this?

venka...@yahoo.com

unread,
Sep 4, 1998, 3:00:00 AM9/4/98
to
In article <Pine.GSO.3.96.980901224647.26831D-100000@dilbert>,
Anil - Sehgal <ase...@cs.tamu.edu> wrote:
>
> We can pass an object to a function as a pointer or as a reference.
>
> Can anybody explain about what are the relative advantages / disadvantages
> of using references over pointers. Internally references are also passed
> as address in some way or the other.
>
> Thanks,
>
> ANil Sehgal
>
>

One thing is that program readability is there while using references.
The disadvantage is that if somebody else is reading the program
he is not sure of the called program signature like whether it
is reference to a object or reference to a const object or it is
simply a ObjectType obj(ie. passed by val). If it is passes by
pointer we are sure that either it is going to change or it is
for stack efficiency the structure pointer is passed. If it is in the
case of primitive type mostly we can tell that the value pointed to is
going to be changed.

Thanks.

Regards,

Venkatesh

Chris

unread,
Sep 4, 1998, 3:00:00 AM9/4/98
to
> Anil - Sehgal <ase...@cs.tamu.edu> wrote:
> > We can pass an object to a function as a pointer or as a reference.
> > Can anybody explain about what are the relative advantages / disadvantages
> > of using references over pointers. Internally references are also passed
> > as address in some way or the other.

References are usually used to pass "in" arguments by reference instead
of by value. A reference is a limited pointer: its advantage over a
real
pointer is that you have fewer chances to shoot yourself in the foot by
saying:

val = ptr; // where you meant value = *ptr
ptr++; // where you meant (*ptr)++
ptr = 0; // where you meant *ptr = 0

since the reference will "stay out of the way":

val = ref; // means val = object
ref++; // means object++
ref = 0; // means object.operator=(0)

The combination of "const Class&" is a nice, safe way to pass a
read-only
reference argument to an object.

However, I would use a real pointer for an "in-out" or "out" arguments.
The caller must specify '&thing' and this makes it clear to him/her that
the routine may modify thing, E.g.

X thing;
object.operation(35, &thing); // clear here that thing is changed

rather than using a reference, which the caller does not see:

X retval;
object.operation(35, thing); // is thing passed in or out?


ethu...@my-dejanews.com wrote:
> In article <Pine.GSO.3.96.980901224647.26831D-100000@dilbert>,
> How a reference is implemented depends on the compiler. I think many, or
> perhaps most compiler makers implement references as handles. Now, 'What's a
> handle...' This is also dependent on implementation, but it is usually a
> double- indirection pointer, a pointer to a pointer to the 'stuff' you are

> interested in. The advantage of this double indirection is that an object in
> memory may have many other objects referring to it. If this object changes
> its location in memory for any reason, with a handle, it only needs to update
> one pointer in a list of pointers that the other objects are referring to so
> that all references remain valid, rather than having some method of tracking
> down all objects that refer to it.

Double indirection? Are you sure? I don't think this is right.

A reference is a single 'limited pointer'. If we take an example of

Object& ref = object;,

then a reference differs from a pointer in that:

- uses . syntax instead of ->
- you cannot find the address of the reference (&ref gives &object)
- you cannot do arithmetic on it (ref++ means object.operator++)
- it's not interchangeable with arrays (ref[12] means
object.operator[])
- a reference must be initialised, and can never be reassigned
(ref = object2 means object.operator=(object2))

But, behind the scenes, the reference is implemented with a single
pointer.
Not a double pointer as far as I know. If it is, perhaps Eric can post
an example of this, and how the object moving is done.

regards,

Chris

Andrei Alexandrescu

unread,
Sep 7, 1998, 3:00:00 AM9/7/98
to

ethu...@my-dejanews.com wrote in message
<6smibi$rui$1...@nnrp1.dejanews.com>...

>How a reference is implemented depends on the compiler. I think many, or
>perhaps most compiler makers implement references as handles. Now, 'What's
a
>handle...' This is also dependent on implementation, but it is usually a
>double- indirection pointer, a pointer to a pointer to the 'stuff' you are
>interested in. The advantage of this double indirection is that an object
in
>memory may have many other objects referring to it. If this object changes
>its location in memory for any reason, with a handle, it only needs to
update
>one pointer in a list of pointers that the other objects are referring to
so
>that all references remain valid, rather than having some method of
tracking
>down all objects that refer to it.


*** The far most common implementation in C++ of a reference is a pointer.
Not a pointer to pointer nor a handle. It's just a pointer that comes with
the star already stuck on it.
I think you entered Java and garbage collection here, but that's another
movie.

Andrei


Statux

unread,
Sep 9, 1998, 3:00:00 AM9/9/98
to
Sheesh... has anyone ever heard of the word: brief?
Andrei Alexandrescu wrote in message <35f3f...@10.1.1.65>...
0 new messages