Re: allocate memory to Char **
Sylvain Henry <[email protected]> Tue, 29 Apr 2014 13:58:06 +0200
| Newsgroups | gmane.comp.lang.haskell.ffi |
|---|---|
| Message-ID | <CAPmptcUOnJxxv0mE0Phahd-86fMopeXwwSkwdWWCGC_Xuv_nPw@mail.gmail.com> |
--===============3976672447678241042==
Content-Type: multipart/alternative; boundary=20cf30050b22163edb04f82d25e9
--20cf30050b22163edb04f82d25e9
Content-Type: text/plain; charset=UTF-8
Hi,
"varnames" is an array of pointers, you cannot allocate it with mallocBytes
(n*100).
Try something like this (not tested):
mallocList :: [String] -> IO (Ptr CString)
mallocList xs = newArray =<< forM xs g
where
g x = do
b <- mallocBytes 100
pokeCString b x 99
return b
pokeCString :: CString -> String -> Int -> IO ()
pokeCString dst value maxLen = withCStringLen (take maxLen value) $ uncurry
(copyArray dst)
-Sylvain
2014-04-29 13:11 GMT+02:00 Lamine <[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
> _______________________________________________
> FFI mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/ffi
>
--20cf30050b22163edb04f82d25e9
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi,<br><br>"varnames" is an array of pointers, y=
ou cannot allocate it with mallocBytes (n*100).<br><br>Try something like t=
his (not tested):<br><br>mallocList :: [String] -> IO (Ptr CString)<br>
mallocList xs =3D newArray =3D<< forM xs g<br>=C2=A0 where<br>=C2=A0=
=C2=A0=C2=A0 g x =3D do<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 b <- mal=
locBytes 100<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pokeCString b x 99<br>=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return b<br><br>pokeCString :: CString=
-> String -> Int -> IO () <br>
pokeCString dst value maxLen =3D withCStringLen (take maxLen value) $ uncur=
ry (copyArray dst)<br><br>-Sylvain<br></div><div class=3D"gmail_extra"><br>=
<br><div class=3D"gmail_quote">2014-04-29 13:11 GMT+02:00 Lamine <span dir=
=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a>></span>:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #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 =3D ccl_new_array (char *, w);<br>
<br>
int i;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for (i =3D 0; i < w; i++)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 varnames[i] =3D ccl_new_ar=
ray (char, 100);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sprintf (varnames[i], &quo=
t;x%d", i);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>
<br>
I try this code unsing mallocList to (<a href=3D"http://lpaste.net/report/7=
12" target=3D"_blank">http://lpaste.net/report/712</a>)<u></u>:<br>
mallocList :: [CString] -> IO (Ptr CString)<br>
mallocList xs =3D do let n =3D Prelude.length xs<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0p <=
- mallocBytes (n*100)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0forM_ =
(Prelude.zip [0..] xs)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (uncu=
rry (pokeByteOff p))<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return=
p<br>
<br>
let n =3D sizeOf(undefined :: CString)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0allocaArray w $ \var -> do<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xs <- pee=
kArray (w*n) var<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 varnames <=
;- mallocList xs<br>
<br>
I have an error "segmentation fault(core dumped)".<br>
can someone please help me? Thank you.<span class=3D"HOEnZb"><font color=3D=
"#888888"><br>
<br>
Lamine<br>
______________________________<u></u>_________________<br>
FFI mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a><br=
>
<a href=3D"http://www.haskell.org/mailman/listinfo/ffi" target=3D"_blank">h=
ttp://www.haskell.org/<u></u>mailman/listinfo/ffi</a><br>
</font></span></blockquote></div><br></div>
--20cf30050b22163edb04f82d25e9--
--===============3976672447678241042==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
FFI mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/ffi
--===============3976672447678241042==--