FFI and pointers

Discussion in 'Ruby' started by Thorsten Hater, Oct 28, 2009.

  1. Hello,

    I'm playing around with FFI and encountered a problem, I could not solve
    till now.
    Let's assume I use standard C's malloc in my wrapper:

    attach_function :malloc, [:int], :pointer

    so far no problem. But what now? The pointer returned is of type void*,
    which is of no help, since I need double*, int* and float* ...
    Is there a type cast mechanism in FFI (or anything similar) or do I have
    to wrap
    the original library in another layer of C (alternatively write a helper
    lib...)
    Or can I import C's typecasts using FFI itself?

    Best regards,
    Thorsten
    Thorsten Hater, Oct 28, 2009
    #1
    1. Advertising

  2. Thorsten Hater wrote:
    > Hello,
    >
    > I'm playing around with FFI and encountered a problem, I could not solve
    > till now.
    > Let's assume I use standard C's malloc in my wrapper:
    >
    > attach_function :malloc, [:int], :pointer
    >
    > so far no problem. But what now? The pointer returned is of type void*,
    > which is of no help, since I need double*, int* and float* ...
    > Is there a type cast mechanism in FFI (or anything similar) or do I
    > have to wrap
    > the original library in another layer of C (alternatively write a
    > helper lib...)
    > Or can I import C's typecasts using FFI itself?
    >
    > Best regards,
    > Thorsten
    >
    >


    If nothing else, you can use the read_* and write_* methods (like
    read_int) to interact with it.

    -Justin
    Justin Collins, Oct 28, 2009
    #2
    1. Advertising

  3. Well, the problem is not to use the pointer type as such, but
    compatibility issue.
    I have this function behaving like malloc, and other functions
    which take pointers to - let's say - float.
    Since malloc returns void* I suspect the problems I experience
    are due to this mismatch.

    Justin Collins schrieb:
    > Thorsten Hater wrote:
    >> Hello,
    >>
    >> I'm playing around with FFI and encountered a problem, I could not solve
    >> till now.
    >> Let's assume I use standard C's malloc in my wrapper:
    >>
    >> attach_function :malloc, [:int], :pointer
    >>
    >> so far no problem. But what now? The pointer returned is of type void*,
    >> which is of no help, since I need double*, int* and float* ...
    >> Is there a type cast mechanism in FFI (or anything similar) or do I
    >> have to wrap
    >> the original library in another layer of C (alternatively write a
    >> helper lib...)
    >> Or can I import C's typecasts using FFI itself?
    >>
    >> Best regards,
    >> Thorsten
    >>
    >>

    >
    > If nothing else, you can use the read_* and write_* methods (like
    > read_int) to interact with it.
    >
    > -Justin
    >
    Thorsten Hater, Oct 28, 2009
    #3
    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. Spur
    Replies:
    4
    Views:
    2,098
  2. Burton Samograd

    C FFI: easy conversion from list to argv?

    Burton Samograd, Apr 17, 2006, in forum: Python
    Replies:
    1
    Views:
    295
    Burton Samograd
    Apr 17, 2006
  3. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
  4. Charles Oliver Nutter
    Replies:
    60
    Views:
    567
    Sean O'Halpin
    Dec 3, 2008
  5. John Croisant
    Replies:
    0
    Views:
    94
    John Croisant
    Oct 25, 2009
Loading...

Share This Page