Hello!

I have a question about memory management in Chicken for c-string values,
created in an external function.
Consider a small example below. I have main module in Scheme that calls
C function 'c-fun'. 'c-fun' does some work and constructs answer (by
allocating
and filling memory). Then the resulting string passed back to Scheme-part of
the program. Is there any memory loss? Another words, does Chicken free this
memory or I should do it myself. If so, what is the best way?

Thanks in advance,
Andrey

===========================================================================
; main.scm
(declare (foreign-declare "char* c_fun(const char*);"))
(define c-fun (foreign-callback-lambda c-string "c_fun" c-string))

(display (c-fun "Hello, world"))
(newline)
===========================================================================


===========================================================================
/* f-fun.c */
#include <stdlib.h>
#include <string.h>

char* c_fun(char *str)
{
    int size;
    char *res;

    res = (char*)malloc(strlen(str) + 1 + 3);

    strcpy(res, str);
    strcpy(res + strlen(str), "!!!");
    res[strlen(str) + 3] = '\0';

    return res;
}
===========================================================================




_______________________________________________
Chicken-users mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to