Ruby FFI: fixed size char array inside struct

Discussion in 'Ruby' started by daniel åkerud, Oct 28, 2009.

  1. (I dind't find an official Ruby FFI mailing-list so I try here)

    I have a char array baked into a struct (not a pointer), and I have
    problems describing it in a layout.

    struct {
    uint8 Value;
    uint8 String[SIZE_OF_ARRAY];
    } MyArray_t;

    The only thing I've come up with is to have a dummy variable at the end.

    class MyArray_t < FFI::Struct
    layout :Value, :uint8,
    :String, :char_array,
    :Dummy, :uint8, SIZE_OF_ARRAY+1 # TODO TODO

    And I use it something like this:

    arr =
    c_function(arr.pointer) # calls a c function that fills the string
    str_ptr = arr.pointer + arr.offset_of:)String)
    puts str_ptr.read_string()

    I need the dummy otherwise there wont be enough bytes allocated and
    c_function overwrites the heap.
    Any and all help appreciated. (Note that it's a legacy API,
    redesigning the C-interface is not possible.)

    daniel åkerud, Oct 28, 2009
    1. Advertisements

  2. daniel åkerud

    Luis Lavena Guest

    Luis Lavena, Oct 28, 2009
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.