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&#39;t allow to access the memory space addressed?<br>

</div><br><br><div class="gmail_quote">2012/2/12 Antoine Latter <span dir="ltr">&lt;<a href="mailto:aslatter@gmail.com">aslatter@gmail.com</a>&gt;</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 &lt;<a href="mailto:yves.pares@gmail.com">yves.pares@gmail.com</a>&gt; wrote:<br>
&gt; Hello,<br>
&gt;<br>
&gt; According to the documentation<br>
&gt; (<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>
&gt; StablePtrs aims at being opaque on C-side.<br>
&gt; But they provide functions to be casted to/from regular void*&#39;s.<br>
&gt; Does that mean if for instance you have a StablePtr CInt you can cast it to<br>
&gt; Ptr () and alter it on C-side?<br>
&gt;<br>
&gt; void alter(void* data)<br>
&gt; {<br>
&gt;     int* x = (int*)data;<br>
&gt;     *x = 42;<br>
&gt; }<br>
&gt;<br>
&gt; --------------------------------------------------<br>
&gt;<br>
&gt; -- using &#39;unsafe&#39; doesn&#39;t change anything.<br>
&gt; foreign import ccall safe &quot;alter&quot;<br>
&gt;     alter :: Ptr () -&gt; IO ()<br>
&gt;<br>
&gt; main = do<br>
&gt;     sptr &lt;- newStablePtr (0 :: CInt)<br>
&gt;     deRefStablePtr sptr &gt;&gt;= print<br>
&gt;     alter (castStablePtrToPtr sptr)  -- SEGFAULTS!<br>
&gt;     deRefStablePtr sptr &gt;&gt;= print<br>
&gt;     freeStablePtr sptr<br>
&gt;<br>
&gt;<br>
&gt; But I tried it, and it doesn&#39;t work: I got a segfault when &#39;alter&#39; is<br>
&gt; called.<br>
&gt;<br>
<br>
</div>I think that &#39;castStablePtrToPtr&#39; exists because many C APIs use<br>
&#39;void*&#39; to mean &#39;opaque lump of data&#39;, 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>