Get class size of object

Maxim Fomin maxim at maxim-fomin.ru
Sun Aug 11 09:37:19 PDT 2013


On Sunday, 11 August 2013 at 16:16:26 UTC, bearophile wrote:
> Maxim Fomin:
>
>> GC.sizeOf seems to return size of allocated page which is 
>> bigger than needed (for ex. it returns 16 for a new int which 
>> is 4).
>
> The object instance contains a pointer to the virtual table, so 
> there's a way to know what object it is. With that runtime 
> information it should be possible to know the static size of 
> the instance.
>
> Bye,
> bearophile

I do not know how you can calculate precisely object size by 
knowing that GC.sizeOf returns some aligned amount plus knowing 
something about virtual table. In case of simple objects (like 
ints) it does not work since int has no virtual table.

Anyway such guessing is useless since D provides feature to get 
size of class instance (if it is defined) even in comple time 
like enum S = _get_size ...


More information about the Digitalmars-d-learn mailing list