chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] memory management for c-string values created in an


From: Zbigniew
Subject: Re: [Chicken-users] memory management for c-string values created in an external function
Date: Fri, 24 Jun 2005 09:55:18 -0500

Use return type c-string* and chicken will free() the memory for you.

Also, your function does not perform a callback to Scheme, so you can
use foreign-lambda instead of foreign-callback-lambda [which is itself
deprecated in favor of foreign-safe-lambda].

On 6/24/05, Andrey Fomichev <address@hidden> wrote:
> 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
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/chicken-users
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]