Yes, but from C side, a StablePtr is already a void*. (See HsFFI.h which typedefs HsStablePtr to void*)
So its sufficient for a use as an opaque pointer, no need to cast it.
On Sun, Feb 12, 2012 at 8:18 AM, Yves Parès <yves.pares@gmail.com> wrote:I think that 'castStablePtrToPtr' exists because many C APIs use
> Hello,
>
> According to the documentation
> (http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-StablePtr.html),
> StablePtrs aims at being opaque on C-side.
> But they provide functions to be casted to/from regular void*'s.
> Does that mean if for instance you have a StablePtr CInt you can cast it to
> Ptr () and alter it on C-side?
>
> void alter(void* data)
> {
> int* x = (int*)data;
> *x = 42;
> }
>
> --------------------------------------------------
>
> -- using 'unsafe' doesn't change anything.
> foreign import ccall safe "alter"
> alter :: Ptr () -> IO ()
>
> main = do
> sptr <- newStablePtr (0 :: CInt)
> deRefStablePtr sptr >>= print
> alter (castStablePtrToPtr sptr) -- SEGFAULTS!
> deRefStablePtr sptr >>= print
> freeStablePtr sptr
>
>
> But I tried it, and it doesn't work: I got a segfault when 'alter' is
> called.
>
'void*' to mean 'opaque lump of data', and these exist to conform to
that sort of API.
Antoine