Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by Marshall (Canon)
on Nov 26, 2022 at 02:47 UTC ( [id://11148397]=note: print w/replies, xml ) Need Help??


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

Well, these are two completely different functions that have completely different purposes and uses.

void Newx (void* ptr, int nitems, type) void* safemalloc(size_t size)
I show an application for safemalloc() at Allocate a variable length array inside of a struct where Newx() is just not appropriate. Newx() is only appropriate if the sizeof the type is a constant.

Replies are listed 'Best First'.
Re^12: Perl XS binding to a struct with an array of chars*
by syphilis (Archbishop) on Nov 27, 2022 at 02:10 UTC
    Newx() is only appropriate if the sizeof the type is a constant

    That's a good demo of when it makes better sense to use safemalloc() - but it seems that you can still use Newx() if you want.
    Instead of doing:
    message = (EdjeMessageStringSet*) safemalloc( sizeof(EdjeMessageString +Set) + (count+1)*sizeof(char*) );
    I think the following Newx() rendition does the same thing (and does it portably):
    Newx( message, (sizeof(EdjeMessageStringSet) + (count+1)*sizeof(char*) +) / sizeof(char), char );
    Of course, that Newx() approach relies on (sizeof(EdjeMessageStringSet) + (count+1)*sizeof(char*)) being an exact multiple of sizeof(char), which is a pretty safe bet whenever sizeof(char) is 1.

    TBH, I had never considered the possibility of Newx() being called with the first arg being a pointer to a certain type && the final arg specifying a different type.
    I wonder if there's a problem with doing that ... of which I'm currently unaware.
    Live and learn .... ;-)

    UPDATE:
    Another thing that had been nagging at me was "Why does the memory have to be allocated in one hit ?".
    The answer, of course, is "It doesn't". It's quite ok (and makes better sense to me) to do the memory allocations separately:
    char ** p; EdjeMessageStringSet* message; ... Newx(message, 1, EdjeMessageStringSet; Newx(p, count + 1, char*); ...
    and that works quite well.

    Cheers,
    Rob
      Yes, I would use a separate memory allocation assignment in the case of storing a pointer to that array in the struct (a char**) too. The other code that I showed puts the actual array into the structure - there is no pointer to the array because the array is right there.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found