allocate memory to Char **

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

allocate memory to Char **

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
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Reply | Threaded
Open this post in threaded view
|

Re: allocate memory to Char **

Sylvain HENRY
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 <[hidden email]>:

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
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi


_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Reply | Threaded
Open this post in threaded view
|

Re: allocate memory to Char **

Lamine
In reply to this post by Lamine
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
_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Reply | Threaded
Open this post in threaded view
|

Re: allocate memory to Char **

Lamine
In reply to this post by Sylvain HENRY
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 <[hidden email]>:

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
[hidden email]
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

_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Reply | Threaded
Open this post in threaded view
|

Re: allocate memory to Char **

Sylvain HENRY
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 <[hidden email]>:
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 <[hidden email]>:

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
[hidden email]
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


_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi
Reply | Threaded
Open this post in threaded view
|

Re: allocate memory to Char **

Lamine
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 <[hidden email]>:
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 <[hidden email]>:

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
[hidden email]
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

_______________________________________________
FFI mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ffi