C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What a reference truly is?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Southp
Guest





PostPosted: Sun Sep 12, 2004 4:04 pm    Post subject: What a reference truly is? Reply with quote



Recently, my senior and I discussed properties, usages about reference.
Finally, we came up with several questions that we can't certain.
After going through several reference book in my bookcase, I decided to post
our question here(I can't find the answer>"<):

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold the same value
as obj1.
So the main difference between a pointer and a reference is that a pointer
holds the address of an object,
while a reference uses the same address and, of course, owns the same value.

b.) A reference implicitly holds the address of the object assigned to it.

That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.
Although they appears different syntactically, if we ignore the restriction
yield by such a implicity(ex. a reference must be initialized,
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce, etc.),
they are the same.


2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.

b.) foo1 and foo2 are the same.


Thanks for reading this:)
Please help!






[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Dennis May
Guest





PostPosted: Mon Sep 13, 2004 10:25 am    Post subject: Re: What a reference truly is? Reply with quote



Southp wrote:

Quote:
Recently, my senior and I discussed properties, usages about reference.
Finally, we came up with several questions that we can't certain.
After going through several reference book in my bookcase, I decided to post
our question here(I can't find the answer>"<):

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold the same value
as obj1.
So the main difference between a pointer and a reference is that a pointer
holds the address of an object,
while a reference uses the same address and, of course, owns the same value.

b.) A reference implicitly holds the address of the object assigned to it.

That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.
Although they appears different syntactically, if we ignore the restriction
yield by such a implicity(ex. a reference must be initialized,
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce, etc.),
they are the same.

In conceptual terms a pointer is an entity in its own right whereas a
reference is simply a synonym for the entity to which it refers.

Quote:


2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.

b.) foo1 and foo2 are the same.

foo1 is better that foo2 in the most general case. Typically a reference
is implemented as a constant pointer (T* const). However a reference may
not be null, whereas a pointer can be. This means that if T is a base
class of the object actually passed it may be necessary to apply an
offset to the address and in this case the compiler must take special
care not to change the value of a null pointer. For a concrete example,
consider

struct B1 { int x; };
struct B2 { int y; };
struct T : public B1, B2 { int z; }

void f1(B2& ref);
void f2(B2* ptr);

Assuming that int is 4 bytes, a typical memory layout for T would be
x at base + 0
y at base + 4
z at base + 8

In order to pass a T to f1, the compiler must add 4 to the address of T
and it can do this unconditionally since it can assume the address is
nonzero. In order to pass a T* to f2, the compiler must also add 4,
however this time it must actually do

if (address_of(ptr) != 0)
address_of(ptr) += 4;

since it is now legitimate for the pointer to be null. This will result
in larger and slower code.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Antoun Kanawati
Guest





PostPosted: Mon Sep 13, 2004 10:31 am    Post subject: Re: What a reference truly is? Reply with quote



Southp wrote:
Quote:
1.)What a reference truly is, and what is the main difference between a
pointer and a reference?

A pointer is a first class value, which can be stored and mutated and
addressed.

References are generally second class citizens; references are
not addressable, even when they are stored (e.g.: when passed
as arguments, or embedded in objects).

Representation-wise, a reference may be represented as a pointer,
or maybe used as an alias at compile time without a concrete
runtime representation.

Quote:
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold the same value
as obj1.
So the main difference between a pointer and a reference is that a pointer
holds the address of an object,
while a reference uses the same address and, of course, owns the same value.

Not exactly. Obj2's type is T&, while obj1's type is T. There are some
minute variations in semantics around this; under the proper conditions,
these variations can be observed. For example: if you can manage to
make the template type deduction system generate to generate a 'T& &' or
a pointer-to-T& you'll get some interesting error messages.

Quote:
b.) A reference implicitly holds the address of the object assigned to it.

That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.
Although they appears different syntactically, if we ignore the restriction
yield by such a implicity(ex. a reference must be initialized,
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce, etc.),
they are the same.

No. A reference's representation may hold the address of an object.
But, in general, you cannot say so. For example, in the example above,
obj2 will merely be a compile-time alias for obj1 and will have no
concrete runtime existence.

Quote:
2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

Generally, the same. Both require that you pass the address of an
object as an argument. This is one of those cases where a reference
needs a runtime representation. If you invoke the mysteries of
inlining and optimization, it may be possible that the second form
would fare better.
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jeff Flinn
Guest





PostPosted: Mon Sep 13, 2004 10:35 am    Post subject: Re: What a reference truly is? Reply with quote


"Southp" <is91033 (AT) cis (DOT) nctu.edu.tw> wrote

Quote:

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold the same value
as obj1.

I view a reference as being an alias to another value. "A rose is a rose..."

Quote:
So the main difference between a pointer and a reference is that a pointer
holds the address of an object,
while a reference uses the same address and, of course, owns the same
value.

b.) A reference implicitly holds the address of the object assigned to it.

And must be initialized, and may never be reseated during the lifetime of
the 'reference'.

Quote:
That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.
Although they appears different syntactically, if we ignore the
restriction
yield by such a implicity(ex. a reference must be initialized,

This is a semantic difference, not just syntactic.

- Pointers convey a zero or one existence concept.
- References convey an always exists concept.

Quote:
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce,
etc.),
they are the same.


2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.

b.) foo1 and foo2 are the same.

They both have their places. foo1 does not check whether the object exists,
foo2 should check that T* != NULL.

Jeff F



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Sep 13, 2004 7:12 pm    Post subject: Re: What a reference truly is? Reply with quote

In article <ci0qf6$lsu$1 (AT) news (DOT) cis.nctu.edu.tw>, Southp
<is91033 (AT) cis (DOT) nctu.edu.tw> writes
Quote:
1.)What a reference truly is, and what is the main difference between a
pointer and a reference?

My answer is that a reference is (a possibly anonymous) name for an
existing object. It is the fact that the definition of a reference does
not create a new object that is the key feature. OTOH at the definition
point of a pointer, a new object is created to hold the pointer value.
<Snip>

Quote:

2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.

b.) foo1 and foo2 are the same.

In practice are likely to compile to code with similar performance. OTOH
it is often easier to maintain code based on references than code based
on pointers, not least because there is a guarantee that a reference
parameter will have been bound to an object at the point of call whilst
a pointer argument is only bound to a pointer value which may not point
to an actual object.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions:
http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Julien Lamy
Guest





PostPosted: Mon Sep 13, 2004 7:53 pm    Post subject: Re: What a reference truly is? Reply with quote

Southp wrote:
Quote:

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?
My senior and I have different opinions about this:
a.) A reference is actually the same as the object assigned to it.
b.) A reference implicitly holds the address of the object assigned to it.


I'd say that (a) is what a reference is, while (b) is how it is
implemented. A reference is essentially an alias to an object, so that
you can access this object by several ways.

Quote:

2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.
b.) foo1 and foo2 are the same.


This is another implementation issue, the language does not guarantee
anything. FWIW, the efficiency of argument passing is the same for
pointers and references.

Hope this helps.
--
Julien Lamy

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Mon Sep 13, 2004 8:01 pm    Post subject: Re: What a reference truly is? Reply with quote

Southp wrote:
Quote:
Recently, my senior and I discussed properties, usages about reference.
Finally, we came up with several questions that we can't certain.
After going through several reference book in my bookcase, I decided to post
our question here(I can't find the answer>"<):

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?

A reference is a declared value [*] that is bound to an existing
object or function when it is created, unlike a normal declared value
or function signature which is bound to a new object or function.

[*] The standard says it is a "name" but this is misleading. I am
using the non-standard term "declared value" to mean a variable,
member variable, function parameter or return value.

A pointer is an object that can point to any object or function of an
appropriate type or none at all. It need not be initialised and it
can be re-pointed.

Quote:
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold the same value
as obj1.

I would draw a distinction between declared values and the objects
they are bound to - so while obj1 and obj2 refer to the same object,
neither of them *is* the object.

Quote:
So the main difference between a pointer and a reference is that a pointer
holds the address of an object,
while a reference uses the same address and, of course, owns the same value.

b.) A reference implicitly holds the address of the object assigned to it.

That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.
Although they appears different syntactically, if we ignore the restriction
yield by such a implicity(ex. a reference must be initialized,
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce, etc.),
they are the same.

So if we ignore all the differences between them, they are the same?

Seriously, it seems to me that (a) is a semantic view and (b) is a
machine-level view of what references are. They're both roughly
correct on different levels.

Quote:
2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.

b.) foo1 and foo2 are the same.

There is unlikely to be any difference in efficiency of argument
passing. However, when compiling foo1 an implementation may in some
way be able to take advantage of the fact that references are non-
null.

--
Ben Hutchings
Sturgeon's Law: Ninety percent of everything is crap.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gareth Stockwell
Guest





PostPosted: Mon Sep 13, 2004 8:12 pm    Post subject: Re: What a reference truly is? Reply with quote

"Southp" <is91033 (AT) cis (DOT) nctu.edu.tw> wrote

Quote:
2.) Efficency?
void foo1(T&);
void foo2(T*);

I would add another question - when passing small objects (e.g.
built-in types such as int, or iterators), is it more efficient to
pass by reference or by value?

a) void do_something(int);
b) void do_something(const int&);

Gareth

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Mon Sep 13, 2004 8:18 pm    Post subject: Re: What a reference truly is? Reply with quote

"Southp" <is91033 (AT) cis (DOT) nctu.edu.tw> wrote

Quote:
Recently, my senior and I discussed properties, usages about reference.
Finally, we came up with several questions that we can't certain.
After going through several reference book in my bookcase, I decided to post
our question here(I can't find the answer>"<):

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold the same value
as obj1.
So the main difference between a pointer and a reference is that a pointer
holds the address of an object,
while a reference uses the same address and, of course, owns the same value.

b.) A reference implicitly holds the address of the object assigned to it.

That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.
Although they appears different syntactically, if we ignore the restriction
yield by such a implicity(ex. a reference must be initialized,
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce, etc.),
they are the same.


2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

a.) foo1 is better than foo2.

b.) foo1 and foo2 are the same.


Scott Meyers wrote somewhere in his books (if my memory is right) that

reference was a "Sugar Syntax" of T* const, from which I got an
understanding of reference.

"Sugar Syntax" means reference is used as a nickname or an alias of
another object name, while it is interrupted as a const pointer.

int n = 3;
int& n1 = n; // interrupted as int* const n1 = &n;
n1 = 4; // interrupted as *n1 = 4;
cout << n1 << endl; // interrupted as cout << *n1 << endl;
cout << &n1 << endl; // interrupted as cout << &(*n1) << endl;
cout << &n << endl; // looks like &n == &n1,
//as suggested by the original poster

Reference increases readability of your code as in copy constructor,
for example, A(A& a), but no efficiency is increased by it anyway.

I have little confidence that I am right. Please correct me.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Mon Sep 13, 2004 8:34 pm    Post subject: Re: What a reference truly is? Reply with quote

Antoun Kanawati wrote:
Quote:
[...]
2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

Generally, the same. Both require that you pass the address of an
object as an argument.

Somehow I don't think that such requirement is spelled out anywhere.
Or is it?

Quote:
This is one of those cases where a reference
needs a runtime representation. If you invoke the mysteries of
inlining and optimization, it may be possible that the second form
would fare better.

The second form, really? Not the first one, with the reference?
Oh, sorry, I didn't quite notice the "it may be possible" clause.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Mon Sep 13, 2004 8:34 pm    Post subject: Re: What a reference truly is? Reply with quote

"Southp" <is91033 (AT) cis (DOT) nctu.edu.tw> wrote

Quote:
Recently, my senior and I discussed properties, usages about reference.
Finally, we came up with several questions that we can't certain.
After going through several reference book in my bookcase, I decided to post
our question here(I can't find the answer>"<):

1.)What a reference truly is, and what is the main difference between a
pointer and a reference?
My senior and I have different opinions about this:

a.) A reference is actually the same as the object assigned to it. For
example,

T obj1;
T &obj2 = obj1;

The address of the obj1 and obj2 is the same, and obj2 hold
the same value as obj1.

Almost true. obj2 doesn't have an address, and it's not an object.
It refers to the same value. It's really more a name than anything else.

Quote:
So the main difference between a pointer and a reference is that a pointer
holds the address of an object, while a reference uses the same
address and, of course, owns the same value.

No, references don't use addresses or so. You can't call that a
difference.

Quote:
b.) A reference implicitly holds the address of the object assigned to it.

Perhaps. There are situations where a compiler will choose to use
a pointer internally to implement a reference.

Quote:
That is, the main difference between a pointer and a reference is that the
former is explict, while the latter is implicit.

Plus that a reference then (where implemented as a pointer) holds the
address of an object; a pointer can also hold NULL.

Quote:
Although they appears different syntactically, if we ignore the restriction
yield by such a implicity(ex. a reference must be initialized,
a lexical constant can be assigned to a const reference, and there are no
reference-to-reference, array of reference, and pointer to refernce, etc.),
they are the same.

That's pretty much obvious: except where X and Y differ, they're the same
for any X and Y.

Quote:
2.) Efficency?
void foo1(T&);
void foo2(T*);

Considering the efficency of argument passing, which one is better?

Usually T&, as you don't have to deal with the case T*==NULL. Of
course, if you do have to deal with T*==NULL, you don't have a choice.
That's of course not an argument about parameter passing (as
implemented in the compiler) but about parameter processing (as
implemented in the code you wrote).


Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Paul Evans
Guest





PostPosted: Mon Sep 13, 2004 8:44 pm    Post subject: Re: What a reference truly is? Reply with quote

Two points:

1) A reference doesn't necessarily 'hold' anything: "It is unspecified
whether or not a reference requires storage"(8.3.2 p3)

2) A reference can (8.5.3 p5) (ignoring conversions):
a) directly bind to an lvalue
b) bind to the (sub)object "represented" by an rvalue
c) bind to a temporary copy constructed/initialised (sub)object
and the implementation is given some scope as to which binding to
choose.

As you point out, your two options are really one and the same. Also:
they don't cover all the above possible reference contexts.

It is probably unhelpful (unless you're building a compiler) to think
of what a reference "is". Much better to focus on how a reference
"behaves".

Paul

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gary Labowitz
Guest





PostPosted: Tue Sep 14, 2004 12:12 pm    Post subject: Re: What a reference truly is? Reply with quote

"Paul Evans" <paul_evans7 (AT) hotmail (DOT) com> wrote

Quote:
Two points:

1) A reference doesn't necessarily 'hold' anything: "It is unspecified
whether or not a reference requires storage"(8.3.2 p3)

2) A reference can (8.5.3 p5) (ignoring conversions):
a) directly bind to an lvalue
b) bind to the (sub)object "represented" by an rvalue
c) bind to a temporary copy constructed/initialised (sub)object
and the implementation is given some scope as to which binding to
choose.

As you point out, your two options are really one and the same. Also:
they don't cover all the above possible reference contexts.

It is probably unhelpful (unless you're building a compiler) to think
of what a reference "is". Much better to focus on how a reference
"behaves".

Indeed, this is the way out of this non-sense. I can (off hand) think of two
other ways to implement a reference (other than use of a auto-dereference
pointer). But any way it is implemented must function as specified in the
standard. Now, again, how many angels can dance on a reference?
--
Gary



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Tue Sep 14, 2004 10:48 pm    Post subject: Re: What a reference truly is? Reply with quote

Gary Labowitz wrote:
Quote:
[...] Now, again, how many angels can dance on a reference?

That depends on the object the reference references because for
any T

sizeof(T&) == sizeof(T);

, no?

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Wed Sep 15, 2004 6:31 pm    Post subject: Re: What a reference truly is? Reply with quote

In article <vpC1d.2025$Ae.1629 (AT) newsread1 (DOT) dllstx09.us.to.verio.net>,
Victor Bazarov <v.Abazarov (AT) comAcast (DOT) net> writes
Quote:
Gary Labowitz wrote:
[...] Now, again, how many angels can dance on a reference?

That depends on the object the reference references because for
any T

sizeof(T&) == sizeof(T);

, no?
Yes, and no. What sizeof(T&) measures IS the sizeof(T). There is no

available mechanism in C++ to find how much storage is occupied by a
reference, only the object referenced. This is as it should be because
the compiler is free to use as much or as little storage as is necessary
in the current context. For example:

int main(){
int i(0);
int const & i_cref(i);
// other code
}

i_cref provides a read only name for the object created for i. But the
compiler does not need to provide any storage for i_cref it just checks
that where the name is used no attempt is made to change the object
'owned' by i then it goes ahead and uses i_cref as if the programmer had
written i.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.