Theocacao
Leopard
Design Element
Comment on "Intro to C Pointers and Dynamic Memory"
by Bill Tubbs — Oct 30
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."

Thanks, all the tutorials are excellent.
Back to "Intro to C Pointers and Dynamic Memory"
Design Element

Copyright © Scott Stevenson 2004-2015