View Single Post
Old 01-19-2004, 05:30 AM   #6
Kastagaar
Member
 
Join Date: Apr 2002
Location: Hampshire, UK
Posts: 117
Kastagaar is on a distinguished road
Send a message via Yahoo to Kastagaar
[/quote]

A couple of notes. In C, you shouldn't cast the result of malloc (or calloc or realloc). The compiler will do this for you, and it knows better. On a practical note, the cast can hide a warning which indicates that you have forgotten stdlib.h (if, indeed, you have forgotten it). Forgetting this and using malloc causes undefined behaviour.*

My second note is that sprintf in that context can be erroneous. if the text contained in the variable string contains %s or %d or whatever, then this too will be undefined behavious - a possible crash. Use strcpy instead.

*This is because undeclared functions are assumed to return int. One specific case where this would fail is in a machine which has a 2-byte int and a 4-byte pointer. The pointer returned from malloc would be truncated to two bytes before being converted back into a 4-byte pointer with half its information missing.
Kastagaar is offline   Reply With Quote