Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

:double field in union layout #204

Closed
nanki opened this Issue · 1 comment

1 participant

@nanki

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)

union.c

#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]);
    }

    printf("¥n");
}

union.rb

require 'ffi'

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

  class U < FFI::Union
    layout(
      :d, :double,
      :i, :int
    )
  end

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

u = Union::U.new
u[:d] = 1.0

Union.dump_union u
@ghost Unknown closed this issue from a commit
Wayne Meissner Fix initialization of StructLayouts that are unions. Fixes #204. 8e6dc8c
@ghost ghost closed this in 8e6dc8c
@ghost

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.