allocate memory to Char **

Lamine <[email protected]> Tue, 29 Apr 2014 13:11:46 +0200
Newsgroups gmane.comp.lang.haskell.ffi
Message-ID <[email protected]>
Hi,

I want to do write this C code in haskell code, but i have some pb:

int w ;
char **varnames = ccl_new_array (char *, w);

int i;
           for (i = 0; i < w; i++)
             {
               varnames[i] = ccl_new_array (char, 100);
               sprintf (varnames[i], "x%d", i);
             }

I try this code unsing mallocList to (http://lpaste.net/report/712):
mallocList :: [CString] -> IO (Ptr CString)
mallocList xs = do let n = Prelude.length xs
                    p <- mallocBytes (n*100)
                    forM_ (Prelude.zip [0..] xs)
                     (uncurry (pokeByteOff p))
                    return p

let n = sizeOf(undefined :: CString)
            allocaArray w $ \var -> do
                   xs <- peekArray (w*n) var
                   varnames <- mallocList xs

I have an error "segmentation fault(core dumped)".
can someone please help me? Thank you.

Lamine