perl.inline
[Top] [All Lists]

Allocating a char* and returning to Perl from a C function

Subject: Allocating a char* and returning to Perl from a C function
From: pfca@xxxxxxxxxxxxxxx (Paulo Filipe Andrade)
Date: Wed, 30 Jul 2008 17:59:44 +0100
Newsgroups: perl.inline


Hello,

I have a C function that I need to call from Perl.
It goes a little something like this:

char *work(char *inString){
        char *stringToReturn = (char *)malloc(2*sizeof(inString));

        while(*inString){
                // do some work on stringToReturn
                inString++;
        }
        return stringToReturn;
}

The thing is, since I can't free stringToReturn, I need to have it garbage collected in the perl side. I have tried creating an SV * with the newSVpv* methods, passing in the stringToReturn and then freeing it, but I am always getting segmentation faults.

This should be pretty simple, but I just can't get it to work.

Thank you for your time.
Paulo F. Andrade
pauloandrade@xxxxxxxxxx





<Prev in Thread] Current Thread [Next in Thread>