Yes, but from C side, a <span style="font-family:courier new,monospace">StablePtr</span><b> is </b><b>already a </b><b style="font-family:courier new,monospace">void*</b>. (See HsFFI.h which typedefs <span style="font-family:courier new,monospace">HsStablePtr</span> to <span style="font-family:courier new,monospace">void*</span>)<br>
So its sufficient for a use as an opaque pointer, no need to cast it.<br><br><div style="text-align:left">So what is the use of casting it to a <span style="font-family:courier new,monospace">Ptr ()</span> if this doesn't allow to access the memory space addressed?<br>
</div><br><br><div class="gmail_quote">2012/2/12 Antoine Latter <span dir="ltr"><<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Sun, Feb 12, 2012 at 8:18 AM, Yves Parès <<a href="mailto:yves.pares@gmail.com">yves.pares@gmail.com</a>> wrote:<br>
> Hello,<br>
><br>
> According to the documentation<br>
> (<a href="http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-StablePtr.html" target="_blank">http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Foreign-StablePtr.html</a>),<br>
> StablePtrs aims at being opaque on C-side.<br>
> But they provide functions to be casted to/from regular void*'s.<br>
> Does that mean if for instance you have a StablePtr CInt you can cast it to<br>
> Ptr () and alter it on C-side?<br>
><br>
> void alter(void* data)<br>
> {<br>
> int* x = (int*)data;<br>
> *x = 42;<br>
> }<br>
><br>
> --------------------------------------------------<br>
><br>
> -- using 'unsafe' doesn't change anything.<br>
> foreign import ccall safe "alter"<br>
> alter :: Ptr () -> IO ()<br>
><br>
> main = do<br>
> sptr <- newStablePtr (0 :: CInt)<br>
> deRefStablePtr sptr >>= print<br>
> alter (castStablePtrToPtr sptr) -- SEGFAULTS!<br>
> deRefStablePtr sptr >>= print<br>
> freeStablePtr sptr<br>
><br>
><br>
> But I tried it, and it doesn't work: I got a segfault when 'alter' is<br>
> called.<br>
><br>
<br>
</div>I think that 'castStablePtrToPtr' exists because many C APIs use<br>
'void*' to mean 'opaque lump of data', and these exist to conform to<br>
that sort of API.<br>
<span class="HOEnZb"><font color="#888888"><br>
Antoine<br>
</font></span></blockquote></div><br>