Topic: Address of indirection expression
Author: David R Tribble <david@tribble.com>
Date: 1999/12/08 Raw View
Marco Jacques wrote:
>
> Hello,
>
> If we have the following declarations:
>
> int a;
> int *b = &a;
>
> Can I always assume that (by the standard) b == &(*b) ?
Yes, provided that you never reassign the value of b.
> What I want is that the expression '&(*b)' always returns the address
> of 'a'. This works with the compiler I am using (CC on SGI). But,
> since we are planning to port our product to other platforms, I am
> wondering if I can always make this assumption. One possible problem
> that I can see is that the result of '*b' is put in a temporary, and
> then the '&' operator would take the address of the temporary, instead
> of the address of 'a'.
That would mean that the value of b is allowed to change underneath
your feet, which is not allowed (unless you explicitly change b
yourself).
int * b2 = b;
...
if (b == b2) ... // This should always be true
// provided that you don't assign to b
-- David R. Tribble, david@tribble.com, http://david.tribble.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/12/08 Raw View
Marco Jacques wrote:
>
> Hello,
>
> If we have the following declarations:
>
> int a;
> int *b = &a;
>
> Can I always assume that (by the standard) b == &(*b) ?
>
> What I want is that the expression '&(*b)' always returns the address of
> 'a'. This works with the compiler I am using (CC on SGI). But, since
> we are planning to port our product to other platforms, I am wondering
> if I can always make this assumption. One possible problem that I can
> see is that the result of '*b' is put in a temporary, and then the '&'
> operator would take the address of the temporary, instead of the address
> of 'a'.
The expression *b is an lvalue expression that refers to whatever
b points to. Assuming b has been initialized or assigned an address
that is still valid, taking the address of *b yields that address.
It is not possible for the exact sequence above to yield anything
but &a for the expression &*b.
If b is null or uninitialized, or contains an address that is no
longer valid (e.g., the memory was returned to the free store,
or b has the address of an auto object whose lifetime has ended),
the results of *b and &*b are undefined.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: wmm@fastdial.net
Date: 1999/12/09 Raw View
In article <3846863C.3F56C7A6@NO_SPAM_PLEASE.dnasoft.com>,
Marco Jacques <marco@NO_SPAM_PLEASE.dnasoft.com> wrote:
> Hello,
>
> If we have the following declarations:
>
> int a;
> int *b = &a;
>
> Can I always assume that (by the standard) b == &(*b) ?
>
> What I want is that the expression '&(*b)' always returns the address
of
> 'a'. This works with the compiler I am using (CC on SGI). But, since
> we are planning to port our product to other platforms, I am
wondering
> if I can always make this assumption. One possible problem that I can
> see is that the result of '*b' is put in a temporary, and then the '&'
> operator would take the address of the temporary, instead of the
address
> of 'a'.
No, a conforming compiler cannot behave this way. 5.3.1p1 says that
"the result [of unary *] is an lvalue referring to the object or
function to which the [operand] points." Since the & operator takes
an lvalue, there is no lvalue-to-rvalue conversion, and the result
of your expression must be as you wanted it.
--
William M. Miller, wmm@fastdial.net
OnDisplay, Inc. (www.ondisplay.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Marco Jacques <marco@NO_SPAM_PLEASE.dnasoft.com>
Date: 1999/12/07 Raw View
Hello,
If we have the following declarations:
int a;
int *b = &a;
Can I always assume that (by the standard) b == &(*b) ?
What I want is that the expression '&(*b)' always returns the address of
'a'. This works with the compiler I am using (CC on SGI). But, since
we are planning to port our product to other platforms, I am wondering
if I can always make this assumption. One possible problem that I can
see is that the result of '*b' is put in a temporary, and then the '&'
operator would take the address of the temporary, instead of the address
of 'a'.
Thanks in advance for your insights,
Marco Jacques
marco@NO_SPAM_PLEASE.dnasoft.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]