Topic: forcing alignment of classes
Author: nikki@trmphrst.demon.co.uk (Nikki Locke)
Date: Thu, 3 Mar 1994 12:21:36 +0000 Raw View
In article <1994Feb19.105306.67482@qut.edu.au> ifn100patter@qut.edu.au writes:
> Is there a standard (or at least semi-portable) way to set the
> alignment of a certain class to a particular value?
No. Alignment is not portable. Different architectures require different
alignments for different types. Many 16-bit machines align all objects of
16 bits or more on 16-bit boundaries. Many 32-bit machines align all
objects of 32 bits or more on 32-bit boundaries. Some machines will core
dump if any attempt is made to access an object which is not correctly
aligned. Others allow you to turn alignment off altogether.
> Eg: I need to force a particular class to align to 128 bit (4 word)
> boundaries. The class will be used on the stack as well as in
> freestore, so redefining new() to get aligned memory is _not_ enough.
If you are using a 32-bit machine, your compiler probably does this by
default.
> Also, is there a way to force an abstract base class _not_ to be
> padded out so that it's size is a multiple of it's largest member?
>
> Eg: Given
>
> struct base { int a; short b; virtual void f()=0; }; // size == 12
>
> struct derived : base { short c; void f(); }; // size == 16
>
> is there a way to make sizeof(base) == 10, and therefore make
> sizeof(derived) == 12? Note that the derived classes will all have
> naturally aligned sizes (multiples of 4) anyway.
>
Are you sure it is padded out to a multiple of the size of its largest
member ? More likely it is padded out to a multiple of 4.
In any case, it depends on your compiler and machine architecture.
--
Nikki Locke,Trumphurst Ltd.(PC & Unix consultancy) nikki@trmphrst.demon.co.uk
trmphrst.demon.co.uk is NOT affiliated with ANY other sites at demon.co.uk.