Re: allocate memory to Char **
Lamine <[email protected]> Tue, 29 Apr 2014 15:40:11 +0200
| Newsgroups | gmane.comp.lang.haskell.ffi |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--===============6846205076653536408==
Content-Type: multipart/alternative;
boundary="------------010806040805090807050404"
This is a multi-part message in MIME format.
--------------010806040805090807050404
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Hi Sylvain,
thank you for your response! I try to use this 2 lines
allocaArray (w*n) $ \var -> do
xs <- peekArray (w) var
to code
char **varnames =3D ccl_new_array (char *, w);
but it return [CString] not a [String]
On 04/29/2014 01:58 PM, Sylvain Henry wrote:
> Hi,
>
> "varnames" is an array of pointers, you cannot allocate it with=20
> mallocBytes (n*100).
>
> Try something like this (not tested):
>
> mallocList :: [String] -> IO (Ptr CString)
> mallocList xs =3D newArray =3D<< forM xs g
> where
> g x =3D do
> b <- mallocBytes 100
> pokeCString b x 99
> return b
>
> pokeCString :: CString -> String -> Int -> IO ()
> pokeCString dst value maxLen =3D withCStringLen (take maxLen value) $=20
> uncurry (copyArray dst)
>
> -Sylvain
>
>
> 2014-04-29 13:11 GMT+02:00 Lamine <[email protected]=20
> <mailto:[email protected]>>:
>
>
> Hi,
>
> I want to do write this C code in haskell code, but i have some pb:
>
> int w ;
> char **varnames =3D ccl_new_array (char *, w);
>
> int i;
> for (i =3D 0; i < w; i++)
> {
> varnames[i] =3D 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 =3D do let n =3D Prelude.length xs
> p <- mallocBytes (n*100)
> forM_ (Prelude.zip [0..] xs)
> (uncurry (pokeByteOff p))
> return p
>
> let n =3D 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] <mailto:[email protected]>
> http://www.haskell.org/mailman/listinfo/ffi
>
>
--=20
=C2=AB Chaque g=C3=A9n=C3=A9ration doit, dans un =C3=A9tat relatif de cap=
tivit=C3=A9, d=C3=A9couvrir=20
sa mission. Elle a le choix de la remplir ou la trahir=C2=BB. Frantz Fano=
n
--------------010806040805090807050404
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content=3D"text/html; charset=3DUTF-8" http-equiv=3D"Content-Ty=
pe">
</head>
<body bgcolor=3D"#ffffff" text=3D"#000000">
Hi Sylvain,<br>
thank you for your response! I try to use=C2=A0 this 2 lines<br>
<br>
allocaArray (w*n) $ \var -> do<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 xs <- peekArray (w) var<br>
to code <br>
char **varnames =3D 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=3D"mid:[email protected]=
l.com"
type=3D"cite">
<div dir=3D"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 =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 <- mallocBytes 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) $ uncurry (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 <spa=
n
dir=3D"ltr"><<a moz-do-not-send=3D"true"
href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a>></span>:<br>
<blockquote class=3D"gmail_quote" style=3D"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 =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_array (char, 100);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sprintf (var=
names[i], "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
moz-do-not-send=3D"true" href=3D"http://lpaste.net/report/7=
12"
target=3D"_blank">http://lpaste.net/report/712</a>):<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 (uncurry (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 <- peekArray (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>
_______________________________________________<br>
FFI mailing list<br>
<a moz-do-not-send=3D"true" href=3D"mailto:[email protected]=
g"
target=3D"_blank">[email protected]</a><br>
<a moz-do-not-send=3D"true"
href=3D"http://www.haskell.org/mailman/listinfo/ffi"
target=3D"_blank">http://www.haskell.org/mailman/listin=
fo/ffi</a><br>
</font></span></blockquote>
</div>
<br>
</div>
</blockquote>
<br>
<br>
<div class=3D"moz-signature">-- <br>
=C2=AB Chaque g=C3=A9n=C3=A9ration doit, dans un =C3=A9tat relatif =
de captivit=C3=A9,
d=C3=A9couvrir sa mission. Elle a le choix de la remplir ou la trah=
ir=C2=BB.
Frantz Fanon
</div>
</body>
</html>
--------------010806040805090807050404--
--===============6846205076653536408==
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
--===============6846205076653536408==--