 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alan Guest
|
Posted: Tue Dec 14, 2004 2:47 pm Post subject: reading a declaration |
|
|
// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
TIA, Alan
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Tue Dec 14, 2004 2:53 pm Post subject: Re: reading a declaration |
|
|
Alan wrote:
| Quote: | // C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
|
Yes.
| Quote: |
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
|
"ref is a reference to int that refers to (is an alias name for) x".
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Tue Dec 14, 2004 3:26 pm Post subject: Re: reading a declaration |
|
|
In message <10rtv6iil7prg53 (AT) news (DOT) supernews.com>, Alan
<alan (AT) surfbest (DOT) net> writes
| Quote: | // C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
|
"ref is a reference to integer that is bound to integer variable x."
--
Richard Herring
|
|
| Back to top |
|
 |
Alan Guest
|
Posted: Tue Dec 14, 2004 4:35 pm Post subject: Re: reading a declaration |
|
|
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
| Quote: | Alan wrote:
// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
Yes.
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
"ref is a reference to int that refers to (is an alias name for) x".
|
I know what you're saying but "refers to" (or "bound to" as
Richard Herring writes) doesn't really tell the reader what
the relationship is, imo. References are really compiler
undecorated pointers:
// C++ Reference
int x = 0;
int& ref = x;
// C Pointer
int y = 0;
int* ptr = &y;
....
// Reference
cout << "x = " << x << ", ref = " << ref << endl; // x = 0, ref = 0
a++;
cout << "x = " << x << ", ref = " << ref << endl; // x = 1, ref = 1
// Pointer
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 0, *ptr = 0
(*b)++;
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 1, *ptr = 1
"a++" is far more preferable to "(*b)++" but I question the choice of terminology in declaring a reference.
'&' denotes 'address of' (for backward compatibility with C) but in
"int& ref = x" or the syntactical equivalent "int &ref = x" , it doesn't
make any sense if it is read in the context of 'address of'.
imo, "int ref = &x" makes more sense.
I wonder why Bjarne didn't just fix the problems with pointers
(hanging pointers, pointers that don't point to anything, void*
pointers, etc) and then use the compiler to undecorate them ;-)
Alan
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Tue Dec 14, 2004 5:15 pm Post subject: Re: reading a declaration |
|
|
In message <10ru5io9961i46b (AT) news (DOT) supernews.com>, Alan
<alan (AT) surfbest (DOT) net> writes
| Quote: |
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote in message
news:cpmuof$51f$04$1 (AT) news (DOT) t-online.com...
Alan wrote:
// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
Yes.
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
"ref is a reference to int that refers to (is an alias name for) x".
I know what you're saying but "refers to" (or "bound to" as
Richard Herring writes) doesn't really tell the reader what
the relationship is, imo. References are really compiler
undecorated pointers:
|
Except when there's no need for a pointer, as in this case.
| Quote: |
// C++ Reference
int x = 0;
int& ref = x;
|
The compiler is quite likely to emit identical code, not involving any
pointers at all, for "do <something> with x" and "do <something> with
ref", for any or all instances of <something>.
| Quote: |
// C Pointer
int y = 0;
int* ptr = &y;
...
// Reference
cout << "x = " << x << ", ref = " << ref << endl; // x = 0, ref = 0
a++;
cout << "x = " << x << ", ref = " << ref << endl; // x = 1, ref = 1
// Pointer
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 0, *ptr = 0
(*b)++;
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 1, *ptr = 1
"a++" is far more preferable to "(*b)++" but I question the choice of
terminology in declaring a reference.
'&' denotes 'address of' (for backward compatibility with C)
|
Even in C, you could equally well say it denotes "pointer to". It's just
the inverse of *. People approaching C++ from a Java background probably
have the same objections to pointers.
| Quote: | but in
"int& ref = x" or the syntactical equivalent "int &ref = x" , it doesn't
make any sense if it is read in the context of 'address of'.
|
So don't say "address of", say "alias for"
| Quote: |
imo, "int ref = &x" makes more sense.
|
Not in the context of the C and C++ reading-from-inside-to-out,
declaration-matches-use rules.
| Quote: |
I wonder why Bjarne didn't just fix the problems with pointers
(hanging pointers, pointers that don't point to anything, void*
pointers, etc) and then use the compiler to undecorate them ;-)
That's called Java. Or maybe C#. |
--
Richard Herring
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Tue Dec 14, 2004 6:49 pm Post subject: Re: reading a declaration |
|
|
Richard Herring wrote:
| Quote: | // C++ Reference
int x = 0;
int& ref = x;
The compiler is quite likely to emit identical code, not involving any
pointers at all, for "do <something> with x" and "do <something> with
ref", for any or all instances of <something>.
|
The same would be true for the int* ptr, too. I don't see any case where a
compiler would have more potential for optimization with a reference than
with a (constant or verifyably unchanged) pointer.
| Quote: | // C Pointer
int y = 0;
int* ptr = &y;
|
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Tue Dec 14, 2004 8:13 pm Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
| Quote: | // C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
|
'ref' is a reference to the object 'x'. IOW 'ref'
is an alternate name (a.k.a an 'alias') for 'x'.
'ref' is not an object.
-Mike
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Tue Dec 14, 2004 8:18 pm Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
| Quote: |
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
Alan wrote:
// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
Yes.
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
"ref is a reference to int that refers to (is an alias name for) x".
I know what you're saying but "refers to" (or "bound to" as
Richard Herring writes) doesn't really tell the reader what
the relationship is, imo. References are really compiler
undecorated pointers:
|
No. A reference is *not* a pointer. (an implementation might
indeed internally implement references via pointers, but the fact
remains that from a language perspective, a reference is not a
pointer. Note that a pointer is an object, a reference is not.
| Quote: |
// C++ Reference
int x = 0;
int& ref = x;
// C Pointer
int y = 0;
int* ptr = &y;
...
// Reference
cout << "x = " << x << ", ref = " << ref << endl; // x = 0, ref = 0
a++;
cout << "x = " << x << ", ref = " << ref << endl; // x = 1, ref = 1
// Pointer
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 0, *ptr = 0
(*b)++;
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 1, *ptr = 1
"a++" is far more preferable to "(*b)++" but I question the choice of
terminology in declaring a reference.
'&' denotes 'address of' (for backward compatibility with C) but in
"int& ref = x" or the syntactical equivalent "int &ref = x" , it doesn't
make any sense if it is read in the context of 'address of'.
|
Of course not, it's an 'overloaded' use of the & operator. Even C
'overloads' some operators (their meaning depends upon context).
| Quote: |
imo, "int ref = &x" makes more sense.
|
Not to me. '&x' already has the well-defined meaning of
'address of'. A reference is not an address, nor is it
a pointer.
| Quote: | I wonder why Bjarne didn't just fix the problems with pointers
(hanging pointers, pointers that don't point to anything, void*
pointers, etc) and then use the compiler to undecorate them
|
I have no idea what you mean by 'decorate' or 'undecorate'.
-Mike
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Tue Dec 14, 2004 10:01 pm Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
| Quote: | '&' denotes 'address of' (for backward compatibility with C) but in
"int& ref = x" or the syntactical equivalent "int &ref = x" , it doesn't
make any sense if it is read in the context of 'address of'.
|
It's not supposed to. '&', the address-of operator, and '&', the reference
declaration, are completely unrelated. They just happen to use the same
symbol.
| Quote: |
imo, "int ref = &x" makes more sense.
|
It doesn't make any sense to me. 'int ref' defines a plain int and '&' is
the address-of operator.
| Quote: | I wonder why Bjarne didn't just fix the problems with pointers
(hanging pointers, pointers that don't point to anything, void*
pointers, etc) and then use the compiler to undecorate them
|
And how do you suggest he should have "fixed" them?
DW
|
|
| Back to top |
|
 |
Alan Guest
|
Posted: Tue Dec 14, 2004 11:42 pm Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
[snip]
To prove that a reference is really a (fancy) pointer
I have provided part of the ASM listing for the
following C++ program (test5.cpp).
Because some of the std library calls and arguments are
very long and add nothing to the discussion, I have
replaced them with "xxxx" but preserved the added comments.
{ my comments in braces }
1 #include <iostream>
2 using namespace std;
3 int x = 0;
4 int& a = x;
5 int main() {
6 cout << "x = " << x << ", a = " << a << endl;
7 a++;
8 cout << "x = " << x << ", a = " << a << endl;
9 return 0;
10 }
{ assembly listing }
PUBLIC ?x@@3HA ; x
PUBLIC ?a@@3AAHA ; a
_BSS SEGMENT
?x@@3HA DD 01H DUP (?) ; x
?a@@3AAHA DD 01H DUP (?) ; a
_BSS ENDS
; File test5.cpp
; Line 4 { int& a = x; }
push ebp
mov ebp, esp
mov DWORD PTR ?a@@3AAHA, OFFSET FLAT:?x@@3HA ; a, x
pop ebp
ret 0
{ Moves the address (or offset) of 'x' into 'a' i.e. 'a' points to 'x' }
_main PROC NEAR
; Line 5
push ebp
mov ebp, esp
; Line 6 { cout << "x = " << x << ", a = " << a << endl; }
push OFFSET FLAT:xxxx ; std::endl
mov eax, DWORD PTR ?a@@3AAHA ; a
mov ecx, DWORD PTR [eax] { get what 'a' points to }
push ecx { push value on stack }
push OFFSET FLAT:$SG7505
mov edx, DWORD PTR ?x@@3HA ; x
push edx
push OFFSET FLAT:$SG7506
push OFFSET FLAT:xxxx ; std::cout
call xxxx ; std::operator<<
add esp, 8
mov ecx, eax
call xxxx ; std::basic_ostream::operator<<
push eax
call xxxx ; std::operator<<
add esp, 8
mov ecx, eax
call xxxx ; std::basic_ostream::operator<<
mov ecx, eax
call xxxx ; std::basic_ostream::operator<<
; Line 7 { a++; }
mov eax, DWORD PTR ?a@@3AAHA ; a
mov ecx, DWORD PTR [eax] { get the value that 'a' points to }
add ecx, 1 { increment the value }
mov edx, DWORD PTR ?a@@3AAHA ; a
mov DWORD PTR [edx], ecx { write the new value back }
; Line 8 { cout << "x = " << x << ", a = " << a << endl; }
push OFFSET FLAT:xxxx ; std::endl
mov eax, DWORD PTR ?a@@3AAHA ; a
mov ecx, DWORD PTR [eax]
push ecx
push OFFSET FLAT:$SG7649
mov edx, DWORD PTR ?x@@3HA ; x
push edx
push OFFSET FLAT:$SG7650
push OFFSET FLAT:xxxx ; std::cout
call xxxx ; std::operator<<
add esp, 8
mov ecx, eax
call xxxx ; std::basic_ostream::operator<<
push eax
call xxxx ; std::operator<<
add esp, 8
mov ecx, eax
call xxxx ; std::basic_ostream::operator<<
mov ecx, eax
call xxxx ; std::basic_ostream::operator<<
; Line 9 { return 0; }
xor eax, eax
; Line 10
pop ebp
ret 0
_main ENDP
|
|
| Back to top |
|
 |
David White Guest
|
Posted: Wed Dec 15, 2004 12:26 am Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
| Quote: |
"Alan" <alan (AT) surfbest (DOT) net> wrote
[snip]
To prove that a reference is really a (fancy) pointer
I have provided part of the ASM listing for the
following C++ program (test5.cpp).
Because some of the std library calls and arguments are
very long and add nothing to the discussion, I have
replaced them with "xxxx" but preserved the added comments.
{ my comments in braces }
1 #include
2 using namespace std;
3 int x = 0;
4 int& a = x;
5 int main() {
6 cout << "x = " << x << ", a = " << a << endl;
7 a++;
8 cout << "x = " << x << ", a = " << a << endl;
9 return 0;
10 }
{ assembly listing }
PUBLIC ?x@@3HA ; x
PUBLIC ?a@@3AAHA ; a
_BSS SEGMENT
?x@@3HA DD 01H DUP (?) ; x
?a@@3AAHA DD 01H DUP (?) ; a
_BSS ENDS
; File test5.cpp
; Line 4 { int& a = x; }
push ebp
|
[snip]
The assembly listing is irrelevant, and so are your comments on it. A
compiler can implement references however it likes. Using an address to the
referred-to object is obviously a natural choice for a compiler to make on
the machines with which most of us are familiar. We already knew that.
However, as far as the C++ language is concerned, a reference is not a
pointer. It is an alias for an object.
DW
|
|
| Back to top |
|
 |
Gary Labowitz Guest
|
Posted: Wed Dec 15, 2004 1:41 am Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
| Quote: |
"Alan" <alan (AT) surfbest (DOT) net> wrote
[snip]
To prove that a reference is really a (fancy) pointer
I have provided part of the ASM listing for the
following C++ program (test5.cpp).
large snip |
You can't use an implementation as an explanation of a standard. It's the
other way 'round.
Anyway, what's so difficult about reading
int &ref = x;
as "ref is another identifier for the int already declared with identifier
x?"
The compiler nods, and says: okay whenever you use the identifier "ref" I'll
use the value in "x." How I do it is none of your business.
A really smart optimizer would just emit the same code for the following two
assignments:
ref = 10;
x = 10;
No need to use a pointer to dereference the address of x.
But that is up to the implementation. It could instead do all sorts of
lookups to find the address of the value in memory.
My point? The standard says what you can do as a programmer. The compiler is
just a way to get your code into machine language.
--
Gary
|
|
| Back to top |
|
 |
Dave O'Hearn Guest
|
Posted: Wed Dec 15, 2004 3:15 am Post subject: Re: reading a declaration |
|
|
Alan wrote:
| Quote: | "Alan" <alan (AT) surfbest (DOT) net> wrote:
To prove that a reference is really a (fancy) pointer
I have provided part of the ASM listing for the
following C++ program (test5.cpp).
Because some of the std library calls and arguments are
very long and add nothing to the discussion, I have
replaced them with "xxxx" but preserved the added comments.
|
I don't think it's entirely wrong to view references as a form of
pointers, but you need to be a bit clearer than just saying it's a
fancy pointer.
I typically consider a reference to be a pointer that is implicitly
dereferenced in all operations except initialization. This also
implicitly makes all references const (though not TO const), since
there is no way to access the pointer to reassign it; it is always
implicitly dereferenced. You can use the & operator to get the address,
but it produces an rvalue, the same way &(*p) produces an rvalue.
This is not how the Standard describes references, but I believe it is
isomorphic to it, so there is no difference. If someone knows of one,
I'd be interested in hearing it. But note that "the reference may have
no runtime representation" does not count. That is a red herring, as it
is equally true of pointers, and everything else.
--
Dave O'Hearn
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Wed Dec 15, 2004 5:34 am Post subject: Re: reading a declaration |
|
|
"Alan" <alan (AT) surfbest (DOT) net> wrote
| Quote: |
"Alan" <alan (AT) surfbest (DOT) net> wrote
[snip]
To prove that a reference is really a (fancy) pointer
I have provided part of the ASM listing for the
following C++ program (test5.cpp).
|
That is no proof at all. All it proves is how a
particular compiler implements a reference. That
does not change the fact that a reference is not a
pointer. If it were a pointer, after initializing
it, you could make it point somewhere else. But you
cannot.
int i = 42;
int j = 99;
int& r = i
r = j; /* assigns the value of 'i' to 'j',
does *not* cause 'r' to refer to 'j' */
cout << i; /* prints 99, not 42 */
-Mike
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Wed Dec 15, 2004 5:48 am Post subject: Re: reading a declaration |
|
|
"Dave O'Hearn" <daveoh77 (AT) pobox (DOT) com> wrote
| Quote: | Alan wrote:
"Alan" <alan (AT) surfbest (DOT) net> wrote:
To prove that a reference is really a (fancy) pointer
I have provided part of the ASM listing for the
following C++ program (test5.cpp).
Because some of the std library calls and arguments are
very long and add nothing to the discussion, I have
replaced them with "xxxx" but preserved the added comments.
I don't think it's entirely wrong to view references as a form of
pointers,
|
It is wrong. A reference is *not* a pointer. Period.
| Quote: | but you need to be a bit clearer than just saying it's a
fancy pointer.
|
Saying it is *any* kind of pointer is incorrect.
| Quote: |
I typically consider a reference to be a pointer that is implicitly
dereferenced in all operations except initialization.
|
That is not the case.
| Quote: | This also
implicitly makes all references const (though not TO const), since
there is no way to access the pointer to reassign it;
|
There is *no* pointer.
| Quote: | it is always
implicitly dereferenced.
|
Only a pointer (or an iterator) can be dereferenced. A reference
is *not* a pointer.
| Quote: | You can use the & operator to get the address,
|
That will yield the address of the object to which the
reference refers. A reference is not an object, it
has no address.
| Quote: | but it produces an rvalue, the same way &(*p) produces an rvalue.
This is not how the Standard describes references,
|
Of course not, because it's utterly wrong.
| Quote: | but I believe it is
isomorphic to it, so there is no difference.
|
There's a huge difference.
| Quote: | If someone knows of one,
I'd be interested in hearing it.
|
A reference is not a pointer. It's not an object, it has no address.
The same is not true of a pointer.
| Quote: | But note that "the reference may have
no runtime representation" does not count. That is a red herring,
|
Not at all.
| Quote: | as it
is equally true of pointers, and everything else.
|
No it's not. A pointer is an object, and occupies storage.
A reference does not.
-Mike
|
|
| Back to top |
|
 |
|
|
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
|
|