Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Inline::C and NULL pointers

by Marshall (Canon)
on Dec 19, 2021 at 06:44 UTC ( [id://11139717]=note: print w/replies, xml ) Need Help??


in reply to Inline::C and NULL pointers

I really don't have much experience with inline::C, but I took a look at your problem. It appears to me that you really don't want an "always mapping" of undef to NULL pointer because maybe undef in another context might mean a missing int in an array of ints or something else. Here in this particular case, undef means a NULL C pointer to char and I think you have to check for that explicitly.

I wrote some simple test code below. In this case, I used SV* instead of char*. The C code figures out if the SV contains a valid pointer to string or not? I tested with both NULL undef and "asdf" as test inputs - see below. I suspect that something is missing in the case of UTF-8 strings. Also my simple test probably should be more complex because say passing an int would be treated the same as sending undef (the test is just valid string or not?).

Hope that this is on the right track...
Update: I guess the obvious was unsaid: why not use "" instead of undef? A null string is not undef.

use v5.10; use Inline 'C'; say "At least we have compiled a bit of C code!"; my $ret= "HD3"; my $output = enctypex_msname("HD2", undef); say "Output from enctypex_msname() call: '$output', \$ret is '$ret'"; =OUTPUT CASE 1: with: my $output = enctypex_msname("HD2", "asdf"); At least we have compiled a bit of C code! Output from enctypex_msname() call: 'HD2', $ret is 'HD3' HD2 asdf name 0000000002D2BC58 retname 0000000002D2BB68 CASE 2: my $output = enctypex_msname("HD2", undef); At least we have compiled a bit of C code! Output from enctypex_msname() call: 'HD2', $ret is 'HD3' HD2 (null) name 0000000002C93298 retname 0000000000000000 =cut __END__ __C__ unsigned char *enctypex_msname(char *name, SV* retname) { char * retname_string = SvPOK(retname) ? SvPV_nolen(retname) : NUL +L; printf ("%s %s\n",name,retname_string); printf ("%s %p %s %p\n", "name ",name, "retname ",retname_string); return name; }
PS again on the C code itself:  "static unsigned char    msname[256];"could be trouble. There is only one msname array. If you call this function again, your code will return the same address for msname as it did the last time. Also perhaps if(!name) return(NULL); should be if(! *name) return(NULL); "" does not mean NULL it means pointer to array of char, if first char is \0 then string is "empty" - not the same as a NULL pointer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139717]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-28 18:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found