Thanks for your explanation Albert, it makes things clearer.<br><br>So StablePtrs are just useful so that C code can:<br>1) call back into Haskell (through a foreign exported function like <span style="font-family:courier new,monospace">doSomethingWithTheObjectIGaveYou :: StablePtr MyObjectType -&gt; Stuff -&gt; IO ()</span>)<br>

2) store them to return them later to Haskell when prompted (through a foreign imported function like <span style="font-family:courier new,monospace">getObject :: Stuff -&gt; IO (StablePtr MyObjectType)</span>)<br>That&#39;s it?<br>

<br>But then,<br>In use case 1), how can a Haskell function modify the data addressed?<br><br>If StablePtrs cannot have their pointed value modified (either C or Haskell-side), that mostly limits their interest, doesn&#39;t it?<br>

<br><br><div class="gmail_quote">2012/2/12 Albert Y. C. Lai <span dir="ltr">&lt;<a href="mailto:trebla@vex.net">trebla@vex.net</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 12-02-12 09:18 AM, Yves Parès wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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/<u></u>packages/archive/base/4.5.0.0/<u></u>doc/html/Foreign-StablePtr.<u></u>html</a>),<br>


StablePtrs aims at being opaque on C-side.<br>
</blockquote>
<br></div>
The doc multiply warns again and again that StablePtr, as well as whatever Ptr you get from castStablePtrToPtr, are opague (meaningless) to the C side. This is sanctioned by Haskell 2010, and GHC certainly exploits it to the fullest. The following example shows what kind of &quot;pointer&quot; values the C side receives for real (I deliberately do not free anything to show you more possible values):<br>


<br>
#include &lt;stdio.h&gt;<br>
void expose(void *p, void *q)<br>
{<br>
  printf(&quot;%p %p\n&quot;, p, q);<br>
}<br>
<br>
import Foreign.StablePtr<br>
import Foreign.Ptr<br>
main = do<br>
  printout (0 :: Int)<br>
  printout (let x = not x in x)<br>
  printout ([] :: [Integer])<br>
printout :: a -&gt; IO ()<br>
printout thunk = do<br>
  p &lt;- newStablePtr thunk<br>
  expose p (castStablePtrToPtr p)<br>
  -- I deliberately do not free<br>
foreign import ccall expose :: StablePtr a -&gt; Ptr b -&gt; IO ()<br>
<br>
Typically the output is like<br>
0xf 0xf<br>
0x10 0x10<br>
0x11 0x11<br>
Looks more like keys of a lookup table than pointers.<br>
<br>
I do not know what good is castStablePtrToPtr for, given that StablePtr is already translated to C side void*, so that no intermediate Ptr step is necessary. Perhaps there is a story from a historical perspective.<br>
<br>
<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>