"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
Objects with virtual base classes are not always contiguous,
which rules out (1).
The C++ standard requires that the memory for arrays (and vectors)
must be contiguous. This seems to imply that the objects stored
in the array or vector must also use contiguous memory.
What's your definition of 'contiguous' ?
Good question. Consider this:
struct A { int a; };
struct B : public virtual A { int b; };
struct C : public virtual A { int c; };
struct D: public virtual B, public virtual C { ind d; };
D d;
B* bp = &d;
C* cp = &d;
Now bp points to an object of type B and cp points to an object of type C.
Each of these objects has a member named a, and because both objects are
subobjects of the same object (namely d), bp->a and cp->a are the same
object. In other words, &bp->a == &cp->a.
The pointer bp points to an object (*bp) of type B, which has two members: b
(defined directly as part of the type) and a (inherited from the base class
A). I think that at the very least, if you say that *bp is contiguous, you
are saying that no other objects appear between bp->a and bp->b. They might
not occupy adjacent addresses, but there can't be any other objects between
them.
Therefore, if *bp is contiguous, bp->a and bp->b are in adjacent memory with
the possible exception of padding or other compiler-generated bookkeeping.
So is *bp contiguous? Well, the answer is either yes or no. If it's no,
then there is such a thing as a discontiguous object. So let's assume that
the answer is yes and forge on. This yes answer implies that bp->a and
bp->b have no other objects between them.
Now let's look at *cp, the object to which cp points. It has two members,
cp->a and cp->c. Is *cp contiguous?
If *cp is contiguous, cp->a and cp->c must have no other objects between
them. However, cp->a is the same object as bp->a, and we have already
established that there is an object (namely bp->b) adjacent to bp->a.
Therefore cp->c cannot be adjacent to cp->a, and *cp cannot be contiguous!
In other words, I have proven that after executing the code shown above, it
is impossible for bp and cp both to point to contiguous objects, according
to a fairly loose definition of contiguity.