Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: Perl XS binding to a struct with an array of chars*

by NERDVANA (Deacon)
on Nov 24, 2022 at 04:37 UTC ( [id://11148348]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl XS binding to a struct with an array of chars*
in thread Perl XS binding to a struct with an array of chars*

"Renewc" The XSUB-writer's interface to the C "realloc" function, with cast. Memory obtained by this should ONLY be freed with "Safefree". void Renewc(void* ptr, int nitems, type, cast)

So actually not how you commented it. The Renewc function modifies the pointer rather than returning anything.

Basically the OP's new version of the code works because sizeof(int) < 2*sizeof(char*)

The renewc is unneeded though, it could just be

message= (EdjeMessageStringSet*) safemalloc( sizeof(EdjeMessageStringSet) + (count-1)*sizeof(char*) )

For this code, the Newx type casting & sizing behavior is simply the wrong API to try to use.

Replies are listed 'Best First'.
Re^5: Perl XS binding to a struct with an array of chars*
by Marshall (Canon) on Nov 24, 2022 at 05:43 UTC
    Yeah, I remember seeing two different I/F definitions and being confused about which one was "truth". My C environment is a bit boogered at the moment so I wasn't able to run test code.

    But in any event, yes, safemalloc() with an explicit size calculation appears to be the correct API in this situation.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found