FFI and pointers

T

Thorsten Hater

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
 
J

Justin Collins

Thorsten said:
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
 
T

Thorsten Hater

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 said:
Thorsten said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top