Re: Address of two different objects of the same type
Matthias Hofmann <hofmann@anvil-soft.com> wrote:
As far as I understand the C++ standard, pointers to two different
objects of the same type cannot appear at the same address. Now
please take a look at the following code:
#include <iostream>
struct A {};
struct B : public A { A a; };
int main()
{
B b;
A* p = &b;
std::cout << p << std::endl;
std::cout << &b.a << std::endl;
return 0;
}
The output I am getting on Visual C++ 2005 Express Edition is:
0012F3D8
0012F3D8
The C++ standard states:
10/5 [Note: ... A base class subobject may be of zero size (clause 9);
however, two subobjects that have the same class type and that belong to
the same most derived object must not be allocated at the same address
(5.10). ]
5.10/1 ... Two pointers of the same type compare equal if and only if
they ... both point to the same object ...
However, DR73
(http://www.open-std.org/JTC1/sc22/WG21/docs/cwg_defects.html#73)
changes the wording of 5.10/1 to read:
5.10/1 ...Two pointers of the same type compare equal if and only if
they ... both represent the same address...
This seems to no longer prohibit two objects from sharing the same
address, and the note in 10/5 becomes groundless (rememeber that
passages marked as [Note] are non-normative).
See also
http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2000/n1234.pdf
The bottom line is, the example appears conformant under DR73 (which is
incorporated into TC1 and thus is normative), but also seems to go
against the intent of the authors, as embodied in the non-normative 10/5
note.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925