[Haskell-cafe] Global Arrays

John Meacham john at repetae.net
Sat Mar 10 02:25:46 CET 2012

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.

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


More information about the Haskell-Cafe mailing list