[mercury-users] Filling Mercury strings in C
Peter Ross
pro at missioncriticalit.com
Wed Mar 26 00:41:03 AEDT 2003
On Tue, Mar 25, 2003 at 01:18:36PM +0100, Petr Nemec wrote:
> Hello people,
> does anybody know (sure he does :),how to fill the Mercury strings
> in C properly? I use the attached code to read a line and to destroy
> ending \n in C. But it seems the memory for the allocated strings is
> not being freed at all during the computation. Do I allocate right :)
> ?
You need to use MR_NEW(<type to allocate>) or
MR_NEW_ARRAY(<type>, <num>) to allocate your memory on the GC'd heap.
So the line
sDynamic = malloc(nLength);
should be
sDynamic = MR_NEW_ARRAY(MR_Char, nlength);
> :- pred creadstring(int,string).
> :- mode creadstring(in,out) is semidet.
>
> :-pragma
> c_code(creadstring(Pointer::in,String::out),[will_not_call_mercury],
> "
> char buffer[MAX_LINE];
> char* sDynamic;
>
> if(fgets(buffer,MAX_LINE,(FILE*)Pointer) != NULL)
> {
> nLength = strlen(buffer);
> buffer[nLength-1] = '\\0';
> sDynamic = malloc(nLength);
> strcpy(sDynamic,buffer);
> String = sDynamic;
> SUCCESS_INDICATOR = 1;
> }
> else
> SUCCESS_INDICATOR = 0;
> ").
>
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list