[mercury-users] Filling Mercury strings in C

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Mar 26 00:06:03 AEDT 2003

On 25-Mar-2003, Petr Nemec <xanthar at seznam.cz> 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 :)
> ?

In grades that use the Boehm et al conservative collector (*.gc
grades), you can use GC_malloc_atomic() instead of malloc(), and then
the memory should get reclaimed by the garbage collector when it is no
longer referenced.

Another alternative is to call the following macro from the Mercury
runtime system: MR_make_aligned_string_copy(<destination>, <source>).
The parameters both have type "char *"; the first one must be an lvalue.
It makes a copy of the string pointed to by <source> on the Mercury
heap, and stores a pointer to this copy in <destination>.  This macro
should only be called in places where the MR_hp register is valid,
i.e. from within a `pragma foreign_proc' code fragment declared
`will_not_call_mercury'.  This has the advantage of also working with
`--gc accurate' and `--gc none'.

The advantage of the second alternative is that it works with `--gc accurate'
or `--gc none'.

Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
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