On Fri, Jun 22, 2012 at 11:18 AM, Captain Freako <capn.fre...@gmail.com> wrote: > Okay, I get it now. Thanks to all of you for your quick replies! > > So, here's what I need to do: > > My Haskell code gets called by a higher level C function and asked to > initialize itself. > As part of that initialization, I'm expected to return a pointer to an > instance of a particular data structure, which gets created/initialized in > Haskell. > I create a stable pointer to a data structure instance, using newStablePtr, > and return that pointer to the calling C function. > The same C function then, at a later time, calls a different function in my > Haskell code, sending it that very same pointer, and expects that code to > modify the data structure pointed to, in place. > > So, I wrote some code that looks like this: > > secondFunction :: StablePtr DataStructure -> IO () > secondFunction dsPtr = do > ds <- deRefStablePtr dsPtr > theRef <- newIORef ds > writeIORef theRef newDs > > which, of course, didn't work, because I didn't understand the true nature > of IORef. > > So, my question is: How do I do this? That is, how do I modify, in place, a > data structure, which I originally created and made stable, using a pointer > to that structure, which is being passed back to me, by the higher level C > function that is calling me? >
You could use a StablePtr (IORef DataStructure). > Thanks, all! > -db > > > On Fri, Jun 22, 2012 at 8:43 AM, Max Rabkin <max.rab...@gmail.com> wrote: > >> >> On Fri, Jun 22, 2012 at 5:30 PM, Captain Freako <capn.fre...@gmail.com> >> wrote: >> > 12 main = do >> > 13 let theValue = 1 >> > 14 print theValue >> > 15 theValueRef <- newIORef theValue >> > 16 bump theValueRef >> > 17 return theValue >> >> >> theValue is a plain old immutable Haskell variable. "newIORef" creates >> an IORef whose initial value is equal to the argument; it doesn't >> create a pointer to the value or something like that. Change "return >> theValue" to "readIORef theValueRef" to extract the changed value in >> the IORef. >> >> --Max >> > > > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe