Re: allocate memory to Char **
Lamine <[email protected]> Tue, 29 Apr 2014 16:21:18 +0200
| Newsgroups | gmane.comp.lang.haskell.ffi |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format. --===============7404354783252531748== Content-Type: multipart/alternative; boundary="------------050803040002090801050407" This is a multi-part message in MIME format. --------------050803040002090801050407 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable Thank a lot ! it works! Lamine. On 04/29/2014 03:52 PM, Sylvain Henry wrote: > To create the [String] as in your example, you just have to write: > > let names =3D ["x"++ show i | i <- [0..w-1]] > varnames <- mallocList names > > -Sylvain > > > 2014-04-29 15:40 GMT+02:00 Lamine <[email protected]=20 > <mailto:[email protected]>>: > > 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 >> 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= ) >> $ uncurry (copyArray dst) >> >> -Sylvain >> >> >> 2014-04-29 13:11 GMT+02:00 Lamine <[email protected] >> <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 captivit=C3=A9, > d=C3=A9couvrir sa mission. Elle a le choix de la remplir ou la trah= ir=C2=BB. > Frantz Fanon > > --=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 --------------050803040002090801050407 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"> Thank a lot ! it works!<br> Lamine.<br> On 04/29/2014 03:52 PM, Sylvain Henry wrote: <blockquote cite=3D"mid:[email protected]= l.com" type=3D"cite"> <div dir=3D"ltr">To create the [String] as in your example, you jus= t have to write:<br> <br> let names =3D ["x"++ show i | i <- [0..w-1]]<br> varnames <- mallocList names<br> <br> -Sylvain<br> </div> <div class=3D"gmail_extra"> <br> <br> <div class=3D"gmail_quote">2014-04-29 15:40 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;"> <div bgcolor=3D"#ffffff" text=3D"#000000"> Hi Sylvain,<br> thank you for your response! I try to use=C2=A0 this 2 line= s<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> <div class=3D""> char **varnames =3D ccl_new_array (char *, w);<br> <br> </div> but it return [CString] not a [String] <div> <div class=3D"h5"><br> On 04/29/2014 01:58 PM, Sylvain Henry wrote: <blockquote 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 <- malloc= Bytes 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 <span 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 (varnames[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/712" target=3D"_blank">http://lpaste.net/report/71= 2</a>):<br> mallocList :: [CString] -> IO (Ptr CString)<= br> mallocList xs =3D do let n =3D Prelude.length x= s<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=A0alloca= Array 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><fo= nt color=3D"#888888"><br> <br> Lamine<br> _______________________________________________<br> FFI mailing list<br> <a moz-do-not-send=3D"true" href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a><br> <a moz-do-not-send=3D"true" href=3D"http://www.haskell.org/mailman/li= stinfo/ffi" target=3D"_blank">http://www.haskell.org/= mailman/listinfo/ffi</a><br> </font></span></blockquote> </div> <br> </div> </blockquote> <br> <br> </div> </div> <div class=3D""> <div>-- <br> =C2=AB Chaque g=C3=A9n=C3=A9ration doit, dans un =C3=A9= tat relatif de captivit=C3=A9, d=C3=A9couvrir sa mission. Elle a le ch= oix de la remplir ou la trahir=C2=BB. Frantz Fanon </div> </div> </div> </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> --------------050803040002090801050407-- --===============7404354783252531748== 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 --===============7404354783252531748==--