[plt-scheme] "define-cunion" prototype for FFI

From: hendrik at topoi.pooq.com (hendrik at topoi.pooq.com)
Date: Sat Nov 10 15:07:47 EST 2007

On Sat, Nov 10, 2007 at 11:01:11AM -0800, Brian Chin wrote:
> 
> Correct me if my understanding is flawed, but unions are notably  
> simpler than structs, given that most of the alignment issues can be  
> avoided since all of the "fields" have a pointer offset of 0. In that  
> regard, the only remaining issues is ensuring that the union type has  
> the correct size, which should be the maximum size of it's members.  

in C, the sizeof() of a type is not the number of bytes it takes but the 
spacing between adjacent elements if it is packed into an array.  This 
may be different from the actual size of a value of that type.-- 

so it ints are 4-byte aligned,

union foo{ int a; byte b[5]; }

would have a sizeof() of 8, even though the union only takes 5 bytes, 
and

struct bar{union foo c; byte d;}

might only take 6 bytes (with a sizeof() of 8).

All this is far more machine-dependent than you might think.  I've once 
used a machine in which doubles were 4-byte aligned, except when they 
were the first field of a structure (recursively) in which case they 
forced 8-byte alignment of the entire structure.

Yeah.  I thought that was pretty weird.

-- hendrik



Posted on the users mailing list.