|Syntactic Confectionery Delight|
Re^12: Perl XS binding to a struct with an array of chars*by syphilis (Archbishop)
|on Nov 27, 2022 at 02:10 UTC ( #11148400=note: print w/replies, xml )||Need Help??|
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:
I think the following Newx() rendition does the same thing (and does it portably):
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 .... ;-)
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:
and that works quite well.
In Section Seekers of Perl Wisdom