chicken-users
[Top][All Lists]
Advanced

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

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


From: Andrey Fomichev
Subject: [Chicken-users] memory management for c-string values created in an external function
Date: Fri, 24 Jun 2005 18:12:16 +0400

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;
}
===========================================================================






reply via email to

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