<div dir="ltr">Hi,<br><br>"varnames" is an array of pointers, you cannot allocate it with mallocBytes (n*100).<br><br>Try something like this (not tested):<br><br>mallocList :: [String] -> IO (Ptr CString)<br>
mallocList xs = newArray =<< forM xs g<br>  where<br>    g x = do<br>       b <- mallocBytes 100<br>       pokeCString b x 99<br>       return b<br><br>pokeCString :: CString -> String -> Int -> IO () <br>
pokeCString dst value maxLen = withCStringLen (take maxLen value) $ uncurry (copyArray dst)<br><br>-Sylvain<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-04-29 13:11 GMT+02:00 Lamine <span dir="ltr"><<a href="mailto:mohamadoulamine@gmail.com" target="_blank">mohamadoulamine@gmail.com</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi,<br>
<br>
I want to do write this C code in haskell code, but i have some pb:<br>
<br>
int w ;<br>
char **varnames = ccl_new_array (char *, w);<br>
<br>
int i;<br>
          for (i = 0; i < w; i++)<br>
            {<br>
              varnames[i] = ccl_new_array (char, 100);<br>
              sprintf (varnames[i], "x%d", i);<br>
            }<br>
<br>
I try this code unsing mallocList to (<a href="http://lpaste.net/report/712" target="_blank">http://lpaste.net/report/712</a>)<u></u>:<br>
mallocList :: [CString] -> IO (Ptr CString)<br>
mallocList xs = do let n = Prelude.length xs<br>
                   p <- mallocBytes (n*100)<br>
                   forM_ (Prelude.zip [0..] xs)<br>
                    (uncurry (pokeByteOff p))<br>
                   return p<br>
<br>
let n = sizeOf(undefined :: CString)<br>
           allocaArray w $ \var -> do<br>
                  xs <- peekArray (w*n) var<br>
                  varnames <- mallocList xs<br>
<br>
I have an error "segmentation fault(core dumped)".<br>
can someone please help me? Thank you.<span class="HOEnZb"><font color="#888888"><br>
<br>
Lamine<br>
______________________________<u></u>_________________<br>
FFI mailing list<br>
<a href="mailto:FFI@haskell.org" target="_blank">FFI@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/ffi" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/ffi</a><br>
</font></span></blockquote></div><br></div>