4.36. StorableArray

A storable array is a mutable array living in the IO monad, which stores its contents in a contiguous memory block living in the C heap. Elements are stored according to the class Storable (Section 4.35). You can obtain the pointer to the array contents to manipulate elements from languages like C.

It's similar to IOUArray but slower. Its advantage is that it's compatible with C.

The array type constructor is called StorableArray. It must be applied to an index type (of class Ix) and element type (of class Storable), providing an array type which is an instance of class MArray (Section 4.21).

The pointer to the array contents is obtained by withStorableArray. The idea is similar to ForeignPtr (used internally here by the way). The pointer should be used only during execution of the IO action retured by the function passed as argument to withStorableArray:

withStorableArray :: StorableArray i e -> (Ptr e -> IO a) -> IO a

If you want to use it afterwards, ensure that you touchStorableArray after the last use of the pointer, so the array is not freed too early:

touchStorableArray :: StorableArray i e -> IO ()