<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Hi Sylvain,<br>
    thank you for your response! I try to use  this 2 lines<br>
    <br>
    allocaArray (w*n) $ \var -> do<br>
                      xs <- peekArray (w) var<br>
    to code <br>
    char **varnames = ccl_new_array (char *, w);<br>
    <br>
    but it return [CString] not a [String]<br>
    On 04/29/2014 01:58 PM, Sylvain Henry wrote:
    <blockquote
cite="mid:CAPmptcUOnJxxv0mE0Phahd-86fMopeXwwSkwdWWCGC_Xuv_nPw@mail.gmail.com"
      type="cite">
      <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 moz-do-not-send="true"
              href="mailto:mohamadoulamine@gmail.com" target="_blank">mohamadoulamine@gmail.com</a>></span>:<br>
          <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
            0.8ex; border-left: 1px solid rgb(204, 204, 204);
            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
              moz-do-not-send="true" href="http://lpaste.net/report/712"
              target="_blank">http://lpaste.net/report/712</a>):<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>
                _______________________________________________<br>
                FFI mailing list<br>
                <a moz-do-not-send="true" href="mailto:FFI@haskell.org"
                  target="_blank">FFI@haskell.org</a><br>
                <a moz-do-not-send="true"
                  href="http://www.haskell.org/mailman/listinfo/ffi"
                  target="_blank">http://www.haskell.org/mailman/listinfo/ffi</a><br>
              </font></span></blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      « Chaque génération doit, dans un état relatif de captivité,
      découvrir sa mission. Elle a le choix de la remplir ou la trahir».
      Frantz Fanon
    </div>
  </body>
</html>