Re: allocate memory to Char **
Sylvain Henry <[email protected]> Tue, 29 Apr 2014 15:52:30 +0200
| Newsgroups | gmane.comp.lang.haskell.ffi |
|---|---|
| Message-ID | <CAPmptcVCw3nGg0Qp+ntqNtmBeDu2XgvUNHHODcMZ8oOXK6UG0w@mail.gmail.com> |
--===============0514108000765167179== Content-Type: multipart/alternative; boundary=20cf30050b2235618604f82ebecb --20cf30050b2235618604f82ebecb Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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]>: > 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]>: > >> >> 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] >> http://www.haskell.org/mailman/listinfo/ffi >> > > > > -- > =C2=AB Chaque g=C3=A9n=C3=A9ration doit, dans un =C3=A9tat relatif de cap= tivit=C3=A9, d=C3=A9couvrir sa > mission. Elle a le choix de la remplir ou la trahir=C2=BB. Frantz Fanon > --20cf30050b2235618604f82ebecb Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">To create the [String] as in your example, you just have t= o write:<br><br>let names =3D ["x"++ show i | i <- [0..w-1]]<b= r>varnames <- mallocList names<br><br>-Sylvain<br></div><div class=3D"gm= ail_extra"> <br><br><div class=3D"gmail_quote">2014-04-29 15:40 GMT+02:00 Lamine <span = dir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_bla= nk">[email protected]</a>></span>:<br><blockquote class=3D"gmail= _quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:= 1ex"> <u></u> =20 =20 =20 <div 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><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 i= t 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 <span = dir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_bla= nk">[email protected]</a>></span>:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0pt 0pt 0pt 0.8= ex;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 (varna= mes[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 href=3D"http://lpaste.= net/report/712" 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><font color=3D"#888= 888"><br> <br> Lamine<br> _______________________________________________<br> FFI mailing list<br> <a href=3D"mailto:[email protected]" target=3D"_blank">FFI@ha= skell.org</a><br> <a href=3D"http://www.haskell.org/mailman/listinfo/ffi" tar= get=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=A9tat relatif de= captivit=C3=A9, d=C3=A9couvrir sa mission. Elle a le choix de la remplir ou la trahir= =C2=BB. Frantz Fanon </div> </div></div> </blockquote></div><br></div> --20cf30050b2235618604f82ebecb-- --===============0514108000765167179== 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 --===============0514108000765167179==--