Help: writing ffi bindings to a C library

Marcin 'Qrczak' Kowalczyk qrczak@knm.org.pl
Wed, 4 Jun 2003 16:33:41 +0200


Dnia śro 4. czerwca 2003 14:16, Bayley, Alistair napisał:

> Is alloca the idiomatic technique when you want to create a pointer to a
> pointer? Or are there other ways?

It's an idiomatic technique for creating a temporary C object whose lifetime 
is explicit. You could use malloc, but then you have to remember to free it.

alloca is an analogue of local variables in C (they are really allocated on 
GHC heap in GHC or with malloc in other implementations).

> Initially I tried to pass a Ptr (Ptr SomeType), but I couldn't because Ptr
> has no constructors To create values of type Ptr I must use the functions
> in Foreign.Marshal.Alloc (and Foreign.Marshal.Utils). Correct?

More or less yes (there is nullPtr, you can 'foreign import' pointers to 
global C variables, you can return pointers from imported C functions if it 
makes sense...).

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/