From mohamadoulamine at gmail.com Tue Apr 29 11:11:46 2014 From: mohamadoulamine at gmail.com (Lamine) Date: Tue, 29 Apr 2014 13:11:46 +0200 Subject: allocate memory to Char ** Message-ID: <535F88F2.5000706@gmail.com> 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 From hsyl20 at gmail.com Tue Apr 29 11:58:06 2014 From: hsyl20 at gmail.com (Sylvain Henry) Date: Tue, 29 Apr 2014 13:58:06 +0200 Subject: allocate memory to Char ** In-Reply-To: <535F88F2.5000706@gmail.com> References: <535F88F2.5000706@gmail.com> Message-ID: 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 : > > 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 > FFI at haskell.org > http://www.haskell.org/mailman/listinfo/ffi > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mohamadoulamine at gmail.com Tue Apr 29 13:07:54 2014 From: mohamadoulamine at gmail.com (Lamine) Date: Tue, 29 Apr 2014 15:07:54 +0200 Subject: allocate memory to Char ** In-Reply-To: <535F8D47.80106@henning-thielemann.de> References: <535F88F2.5000706@gmail.com> <535F8D47.80106@henning-thielemann.de> Message-ID: <535FA42A.6030108@gmail.com> I try pokeElemOff but i have same error "segmentation fault(core dumped)". Thank for your response! On 04/29/2014 01:30 PM, Henning Thielemann wrote: > Am 29.04.2014 13:11, schrieb Lamine: > >> 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)) > > I guess this must be pokeElemOff. > >> 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. > -- ? 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 From mohamadoulamine at gmail.com Tue Apr 29 13:40:11 2014 From: mohamadoulamine at gmail.com (Lamine) Date: Tue, 29 Apr 2014 15:40:11 +0200 Subject: allocate memory to Char ** In-Reply-To: References: <535F88F2.5000706@gmail.com> Message-ID: <535FABBB.3060209@gmail.com> 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 = 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 = 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 >: > > > 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 > FFI at haskell.org > http://www.haskell.org/mailman/listinfo/ffi > > -- ? 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: From hsyl20 at gmail.com Tue Apr 29 13:52:30 2014 From: hsyl20 at gmail.com (Sylvain Henry) Date: Tue, 29 Apr 2014 15:52:30 +0200 Subject: allocate memory to Char ** In-Reply-To: <535FABBB.3060209@gmail.com> References: <535F88F2.5000706@gmail.com> <535FABBB.3060209@gmail.com> Message-ID: To create the [String] as in your example, you just have to write: let names = ["x"++ show i | i <- [0..w-1]] varnames <- mallocList names -Sylvain 2014-04-29 15:40 GMT+02:00 Lamine : > 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 = 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 = 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 : > >> >> 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 >> FFI at haskell.org >> http://www.haskell.org/mailman/listinfo/ffi >> > > > > -- > ? 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mohamadoulamine at gmail.com Tue Apr 29 14:21:18 2014 From: mohamadoulamine at gmail.com (Lamine) Date: Tue, 29 Apr 2014 16:21:18 +0200 Subject: allocate memory to Char ** In-Reply-To: References: <535F88F2.5000706@gmail.com> <535FABBB.3060209@gmail.com> Message-ID: <535FB55E.4030800@gmail.com> 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 = ["x"++ show i | i <- [0..w-1]] > varnames <- mallocList names > > -Sylvain > > > 2014-04-29 15:40 GMT+02:00 Lamine >: > > 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 = 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 = 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 > >: >> >> >> 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 >> FFI at haskell.org >> http://www.haskell.org/mailman/listinfo/ffi >> >> > > > -- > ? 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 > > -- ? 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: