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
    end

    And I use it something like this:

    arr = MyArray_t.new
    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.)

    /D
     
    daniel åkerud, Oct 28, 2009
    #1
    1. Advertising

  2. daniel åkerud

    Luis Lavena Guest

    Luis Lavena, Oct 28, 2009
    #2
    1. Advertising

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.
Similar Threads
  1. Replies:
    5
    Views:
    549
    Flash Gordon
    Apr 9, 2006
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
  3. Replies:
    8
    Views:
    506
    Bob Hairgrove
    Apr 10, 2006
  4. John Croisant
    Replies:
    0
    Views:
    102
    John Croisant
    Oct 25, 2009
  5. Tuan  Bui
    Replies:
    14
    Views:
    476
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page