Thank you for your irrefragable answer

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] 
> Behalf Of Zbigniew
> Sent: Friday, June 24, 2005 6:55 PM
> To: [email protected]
> Subject: Re: [Chicken-users] memory management for c-string 
> values created inan external function
> 
> 
> 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 <[EMAIL PROTECTED]> 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
> > [email protected]
> > http://lists.nongnu.org/mailman/listinfo/chicken-users
> >
> 
> 
> _______________________________________________
> Chicken-users mailing list
> [email protected]
> http://lists.nongnu.org/mailman/listinfo/chicken-users
> 



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

Reply via email to