Re: Bit-Pattern of Representation of Objects
* Frederick Gotham:
Alf P. Steinbach posted:
Of course, in C++, an object can be really "fancy", and have all sorts
of constructors and overloaded operators, but *ultimately*, the object
itself is stored as a simple sequence of bits in memory.
Is it not a fundamental assumption that these bits are contiguous?
No (although for a POD they are, and in practice they are except when
virtual inheritance is involved).
If we have an object, there's two operators we can apply to it which will
give us all the info about how it's stored in memory:
Address-of operator: &obj
sizeof operator: sizeof obj
Let's say that the former gives us a memory location of:
1032
And that the latter gives us a size of:
8
Can we not therefore assume that the object's bits are dispersed between
the following memory locations:
1032 through 1040
Therefore, if we print out all the bits from 1032 through 1040, are we not
printing out all of the object's bits?
struct Base{ char id; Base( char anId = '?' ): id(anId) {} };
struct A: virtual Base { int x; A(): x( 1 ) {} };
struct B: virtual Base { int y; B(): y( 2 ) {} };
struct Derived: A, B { Derived( char anId ): Base( anId ) {} };
void showBits( B const& o )
{
// Assumption of o being contigous is made.
}
int main()
{
B b;
Derived d;
showBits( b ); // Possibly OK.
showBits( d ); // Definitely bad.
}
--
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?