Here's a Green Card problem with Hugs 1.4, April 97 beta. If you try to
pass a Haskell string which is long down to a C function via green card,
you can get a stack overflow. This is because the string gets pushed
onto the stack character by character. As this is done, the size of the
string gets counted. Also each character gets eval'ed. When the whole
thing has been pushed, a C char * is allocated with the result of the
counting, and the characters are transferred off the stack into it. So
if you have a big string, you will run out of stack.

I get round this with function (made using %asm etc) which converts a
Haskell String to a Ptr. It allocates a chunk of memory and then starts
moving characters one by one without pushing them all first. If it runs
out of memory in the chunk, it reallocates it and keeps going.

The problem also occurs passing a string back up from C to Haskell. My
solution here is to pass up a Ptr to the C char *, and then call anoth
%asm'ed function which take a Ptr and an offset and returns a String
which is as much as it could get on the stack (well, actually a
hard-wired number of characters), and an offset for the next bit. You
keep going until there's no more String to get.

Neither solution is very nice, but they solve the problem pro tem.

-- David

_______________________________________________________________________
David Elworthy <[EMAIL PROTECTED]>
Canon Research Centre Europe Ltd., Guildford, Surrey, UK
URL: http://www.cre.canon.co.uk/
Phone: +44 1483 448844; Fax: +44 1483 448845

Reply via email to