Board index » cppbuilder » internal classes

internal classes


2005-10-27 10:11:17 PM
cppbuilder44
Where can I find the internal descriptions of the c++ (at least cbuilder
implementation)
1) structure of classes/instances ?
I have an app with a very very great amount of instances, and im counting
bytes for instances
ex:
with bc6, #pragma pack(1) works on classes. can I assume this is a
standard ?
ex:
class A {}; // sizeof give 8
class B {}; // sizeof give 8
class C { public: int x1; int x2; }; // sizeof give 8, as empty classes,
so I suppose 8 is the min without packing
class D : public A {}; // sizeof give 8
class E: public A { int x1; int x2; virtual void init() {}; }; // sizeof
give 12, ok ptr to vmt added
class F: public A { int x1; int x2; virtual void init1() {}; virtual void
init2(); }; // sizeof give 12, ok only 1 vmt ptr needed
class G: public A, public B {}; // all empty, sizeof give 16 ! why ? this
seems useless ! <<<<<<<<
class H: public A, public B { virtual void init(); }; // same as G with a
virtual, sizeof give 24 ! this is by far a bit too much for me !
I dont show all, as virtual inheritance, packed/not packed etc
2) prolog/epilog asm
in order to use {*word*192} functions
 
 

Re:internal classes

"py" < XXXX@XXXXX.COM >wrote:
Quote
Where can I find the internal descriptions of the c++ (at least cbuilder
implementation)
I can't particularly help you there, as it differs (in theory, greatly)
between implementations.
Quote
1) structure of classes/instances ?
I have an app with a very very great amount of instances, and im counting
bytes for instances
ex:
with bc6, #pragma pack(1) works on classes. can I assume this is a
standard ?
Nope. #pragma is a standard way of telling an implementation to do
something implementation defined.
Quote
ex:
class A {}; // sizeof give 8
class B {}; // sizeof give 8
I get 1 with #pragma pack(1) in BCB5. But 8 is the default size, as it's
much much faster to access DWORD aligned objects.
And even an empty class must have some size - as all distinct objects
must have unique memory addresses in C and C++.
Quote
class C { public: int x1; int x2; }; // sizeof give 8, as empty classes,
so I suppose 8 is the min without packing
Nope. Minimum size _without packing_ can be 1, if the only member is a
char.
Quote
class D : public A {}; // sizeof give 8
class E: public A { int x1; int x2; virtual void init() {}; }; // sizeof
give 12, ok ptr to vmt added
Yes.
Quote
class F: public A { int x1; int x2; virtual void init1() {}; virtual void
init2(); }; // sizeof give 12, ok only 1 vmt ptr needed

class G: public A, public B {}; // all empty, sizeof give 16 ! why ? this
seems useless ! <<<<<<<<
You have already established that A and B are each of size 8. Therefore,
it shouldn't be surprising that C, which contains one of each, is twice
that size.
Yes, contains. Inheritance is syntactic sugar for composition. Given
struct A {
void f();
};
struct B : A {
};
B b;
b.f();
this is equivalent (in memory layout) to
struct A {
void f();
};
struct B {
A __unnamedA;
};
B b;
b.__unnamedA.f();
Presumably you are using empty base classes purely for mixin
implementation. If so, you may find that giving each a single char
member causes them to be char aligned instead. Alternatively, try using
packing options and/or empty base class optimisation (a compiler switch,
I forget which).
Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK