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 inan e


From: Andrey Fomichev
Subject: RE: [Chicken-users] memory management for c-string values created inan external function
Date: Mon, 27 Jun 2005 12:26:09 +0400

Thank you for your irrefragable answer

> -----Original Message-----
> From: address@hidden 
> [mailto:address@hidden 
> Behalf Of Zbigniew
> Sent: Friday, June 24, 2005 6:55 PM
> To: address@hidden
> 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 <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
> >
> 
> 
> _______________________________________________
> 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]