Storable tuples and what is 'alignment'?

Fergus Henderson fjh@cs.mu.OZ.AU
Fri, 9 Aug 2002 17:57:42 +1000


On 06-Aug-2002, Alastair Reid <alastair@reid-consulting-uk.ltd.uk> wrote:
> 
> Andrew J Bromage <ajb@spamcop.net> writes:
> > This number is called the "alignment", and a good rule of thumb for
> > computing it is:
> 
> >  instance Storable a where alignment a = sizeOf a `min` machine_word_size
> 
> The way we calculate it in GHC and Hugs is:
> 
>   #define offsetof(ty,field) ((size_t)((char *)&((ty *)0)->field - (char *)(ty *)0))

You shouldn't define offsetof() yourself.  The C standard provides
offsetof() in <stdlib.h> -- you should use that rather than defining
it yourself.  Defining offsetof() yourself is an error if <stdlib.h>
is included, because you are stepping on the implementation's namespace.
Furthermore, the definition there is not standard-conforming C code,
since it dereferences a null pointer.

-- 
Fergus Henderson <fjh@cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.