optional reduction of overhead for classes

clayasaurus clayasaurus at gmail.com
Mon Apr 17 17:29:58 PDT 2006


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?

> 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