Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I guess I should probably do the obvious thing and just try it out

That could be informative ;-)
I've had a bit of a fiddle with it in Inline::C, and it seems that it probably almost works as you want.
The print string_reverse("\nHello World"); works exactly as expected and does not call free().
And the 2nd line of perl code (string_reverse(undef);) definitely calls free().

The only problem is that having called free(), Inline::C/XS evaluates the condition input == NULL as FALSE - and it therefore allocates more memory, which in turn would require a third call to be cleared ... and so on.
With Inline::C/XS, the argument to the second call needs to be something string_reverse() will evaluate as NULL - and undef does not fit that bill.
Maybe, with FFI::Platypus, undef will be evaluated as NULL ... you could verify that it's being evaluated as desired, by using Devel::Peek and changing that second line to Dump(reverse_string(undef);.

If that results in a Dump() that looks something like:
SV = NULL(0x0) at 0x22cfb41ffb0 REFCNT = 1 FLAGS = (PADTMP)
then the undef was evaluated as NULL and all is probably good.
But for me, with Inline::C, that Dump() produces:
SV = PV(0x22cfb55afc8) at 0x22cfb43c068 REFCNT = 1 FLAGS = (PADTMP,POK,pPOK) PV = 0x22cfd20c3d8 ""\0 CUR = 0 LEN = 10
which indicates that more memory was allocated by that second call.

I remember that this problem with undef not being equated with NULL has come up here before ... I haven't done the search for it :-(
UPDATE: The thread I was thinking of is this one.

I can't recall whether there exists a perl arg that will be equated to NULL, or whether you have to hack the perl typemap to achieve it.
Anyway ... FAIRK, FFI::Platypus might handle undef differently.

Cheers,
Rob
use strict; use warnings; use Devel::Peek; use Inline C =><<'EOC'; #include <string.h> #include <stdlib.h> const char * string_reverse(const char *input) { static char *output = NULL; int i, len; if(output != NULL) { free(output); printf("YES - output free()ed\n"); } if(input == NULL) return NULL; len = strlen(input); output = malloc(len+1); for(i=0; input[i]; i++) output[len-i-1] = input[i]; output[len] = '\0'; return output; } const char * nullstr() { return NULL; } EOC print string_reverse("\nHello World"); Dump(string_reverse(undef)); Dump(nullstr()); __END__ Outputs: dlroW olleH Use of uninitialized value in subroutine entry at try.pl line 42. YES - output free()ed SV = PV(0x22b16cbafc8) at 0x22b16bdcba8 REFCNT = 1 FLAGS = (PADTMP,POK,pPOK) PV = 0x22b19158598 ""\0 CUR = 0 LEN = 10 SV = NULL(0x0) at 0x22b16bbfed0 REFCNT = 1 FLAGS = (PADTMP)

In reply to Re^3: FFI::Platypus: Replace malloc with GC_MALLOC? by syphilis
in thread FFI::Platypus: Replace malloc with GC_MALLOC? by karlgoethebier

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found