
No, you can do nothing with the pointer on the C side other than pass
it back into haskell. It may not even be a pointer, it may be an index
into an array deep within the RTS for instance. The reason they can be
cast to void *'s is so you can store them in C data structures that
don't know about haskell, which tend to take void *s.
John
On Sun, Feb 12, 2012 at 6:18 AM, Yves Parès
Hello,
According to the documentation (http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-St...), 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.
Is it normal? Does this mean I can only use my pointer as opaque? (Which I know to be working, as I already got a C function call back into Haskell and pass it the StablePtr via a 'foreign export') But in that case, what is the use of castStablePtrToPtr/castPtrToStablePtr, as you can already pass StablePtrs to and from C code?
Thanks!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe