[Haskell-cafe] Global Arrays

Clark Gaebel cgaebel at csclub.uwaterloo.ca
Sat Mar 10 02:49:06 CET 2012


What's the advantage of using D.A.Storable over D.Vector? And yes,
good call with creating an array of HSDouble directly. I didn't think
of that!

On Fri, Mar 9, 2012 at 8:25 PM, John Meacham <john at repetae.net> wrote:
> On Fri, Mar 9, 2012 at 12:48 PM, Clark Gaebel
> <cgaebel at csclub.uwaterloo.ca> wrote:
>> static const double globalArray[] = { huge list of doubles };
>> double* getGlobalArray() { return globalArray; }
>> int        getGlobalArraySize() { return
>> sizeof(globalArray)/sizeof(globalArray[0]); }
>>
>> And importing it in haskell witht he FFI, followed with an unsafeCast:
>>
>> foreign import ccall unsafe "getGlobalArray" c_globalArray :: Ptr CDouble
>> foreign import ccall unsafe "getGlobalArraySize" c_globalArraySize :: CInt
>
> You can use Data.Array.Storable to do this.
> http://hackage.haskell.org/packages/archive/array/0.3.0.3/doc/html/Data-Array-Storable.html
>
> Also, there is no need to create stub C functions, you can foreign import
> the array directly
> And if you don't want to cast between CDouble and Double you can declare
> your array to be of HsDouble and #include "HsFFI.h"
>
> const HsDouble globalArray[] = { huge list of doubles };
> foreign import ccall unsafe "&globalArray" :: Ptr Double
>
>    John
>



More information about the Haskell-Cafe mailing list