circuitcellar.com
Magazine Support   Digital Library   Products & Services   Suppliers Directory 
 
 





 
Test Your EQ #145— Answer

Answer 12

In the first function, the buffer for the string is allocated on the stack. This storage is reclaimed when the function returns, and can be overwritten at any time.

The second function uses malloc() to allocate the buffer for the string on the heap. This storage remains allocated after the function returns, and indeed until it is explicitly freed. The second function is more robust, with the caveat that the caller must free the storage before destroying the pointer to it. Otherwise, the program will exhibit a "memory leak."

Contributor: Naveen PN

Published: August-2002

   

E-mail eq@circuitcellar.com with questions or comments.

Back to Questions