optional reduction of overhead for classes

Don Clugston dac at nospam.com.au
Tue Apr 18 06:43:09 PDT 2006


clayasaurus wrote:
> Tom S wrote:
>> clayasaurus wrote:
>>> class EntityData
>>> {
>>>   float x, y;
>>> }
>>>
>>> ------------------------------------------------------------------------
>>>
>>> My question is, how much memory does EntityData use before allocation 
>>> (4-bytes?), and how much does it use after allocation (4+4+4 bytes 
>>> total?).
>>
>> Well, before allocation, your EntityData doesn't really exist, only a 
>> null reference, which takes 4 bytes on 32 bit systems. 
> IIRC, you can
>> check the size of an object thru .classinfo.init.length.
> 
> Thanks for that :) After I allocate it, it says it uses 16 bytes. Since 
> the size of a float is only four bytes and the null reference is  4 
> bytes, would that be 4+4+4 = 12? I wonder where the other 4 bytes come 
> from, maybe typeinfo?

Try two floats, to see if it's just alignment padding.

> 
>> If you want to avoid overhead, consider making EntityData a struct, to 
>> which you'd store a pointer.
> 
> Ok, you mean like MyStruct *str = new MyStruct?
> Thanks.
> 
> ~ Clay



More information about the Digitalmars-d-learn mailing list