Ptr and ForeignPtr Questions

Manuel M. T. Chakravarty chak@cse.unsw.edu.au
Fri, 21 Sep 2001 12:32:16 +1000


Ashley Yakeley <ashley@semantic.org> wrote,

> At 2001-09-20 06:55, Manuel M. T. Chakravarty wrote:
> 
> >The FFI does not ensure any type consistency between the
> >arguments to `Ptr'/`ForeignPtr' and the corresponding C
> >types.
> 
> I've been using 'Ptr Word8' with newArray to pass lists of bytes to C 
> functions. They appear as unsigned char arrays in the C function. Is this 
> wrong, or not guaranteed?

It is guaranteed that when you marshal a `[Word8]' into a
`Ptr Word8' with newArray that you get a contiguous memory
area filled with the bytes from the list.  So, what you are
doing is perfectly fine.  

What I meant with the remark that you quote is that if you
would use

  foreign import foo :: Ptr Int -> IO Float

with

  float foo (float *x)
  {
    return *x;
  }

the system will not complain, but your program may dump
core.

Manuel