Skip to content

:double field in union layout #204

nanki opened this Issue May 7, 2012 · 1 comment

1 participant

nanki commented May 7, 2012

With union.c and union.rb

$ clang union.c -fPIC -shared -o libunion.dylib
$ ruby union.rb

I expected the script to result 00 00 00 00 00 00 f0 3f, but got f0 e4 d2 05 01 00 00 00 (changing randomly each time).

It seems to happen when a :double field appears on the top of the layout.

Tested on

  • Mac OS X 10.7.3
  • ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.3.0]
  • ffi (1.0.11)


#include <stdio.h>

typedef union {
    double d;
    int i;
} union_t;

void dump_union(union_t u) {
    unsigned char *c = (unsigned char *)&u;

    int i;
    for (i = 0; i < sizeof(union_t); i++) {
        printf("%02x ", c[i]);



require 'ffi'

module Union
  extend ::FFI::Library
  ffi_lib 'union'

  class U < FFI::Union
      :d, :double,
      :i, :int

  attach_function :dump_union, [U.by_value], :void

u =
u[:d] = 1.0

Union.dump_union u
@ghost Unknown added a commit that closed this issue May 9, 2012
Wayne Meissner Fix initialization of StructLayouts that are unions. Fixes #204. 8e6dc8c
@ghost ghost closed this in 8e6dc8c May 9, 2012
ghost commented May 9, 2012

It wasn't just doubles in unions, it was union-by-value parameters as a whole were broken. I think it is fixed now, but could do with some more testing.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.