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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.