Class Instance allocations

Simen kjaeraas simen.kjaras at gmail.com
Wed Jan 13 14:50:16 PST 2010


Justin Johansson <no at spam.com> wrote:
>>  If I recall correctly, doubles require 8-byte alignment. So you object  
>> therefore must require 8-byte alignment.
>
> Instances of my Foo class require 17 bytes so therefore all objects  
> require 17 byte alignment.  But for my Bar class it's 42.
>
> ???

If a member of your class requires n-byte alignment, your class will
require (k*n)-byte alignment (where k is a positive integer), to ensure
the member has its required alignment.

Example: doubles require 8-byte alignment. You create
class foo { double bar; }. If your foo is offset by 2 bytes from an
8-byte border, so is bar, and thus its alignment is broken.

-- 
Simen



More information about the Digitalmars-d mailing list