I agree with Herb Payerl's comment above (Jan 22, 07 3). The thing missing from the explanation of the final example is an explanation of the asprintf function.
When reading the code I just couldn't see how the calloc function could possibly be reserving enough memory. As far as I could see it reserves space for 5 pointers, not space for 5 entire strings. Not knowing that aspintf also allocates memory I couldn't see where the string data was being stored. Since the danger with pointers is overwriting memory that hasnt been allocated this was causing me some consternation!
I found this on the web. Hope it helps:
"The asprintf function is nearly identical to the simpler sprintf, but is much safer, because it dynamically allocates the string to which it sends output, so that the string will never overflow."
by Bill Tubbs — Oct 30
When reading the code I just couldn't see how the calloc function could possibly be reserving enough memory. As far as I could see it reserves space for 5 pointers, not space for 5 entire strings. Not knowing that aspintf also allocates memory I couldn't see where the string data was being stored. Since the danger with pointers is overwriting memory that hasnt been allocated this was causing me some consternation!
I found this on the web. Hope it helps:
"The asprintf function is nearly identical to the simpler sprintf, but is much safer, because it dynamically allocates the string to which it sends output, so that the string will never overflow."
Thanks, all the tutorials are excellent.