Re: Maybe we should use "snprintf" instead of "sprintf".

Shaw Terwilliger (sterwill@postman.abisource.com)
Tue, 15 Jun 1999 09:30:46 -0500


Ming-I Hsieh wrote:
> Ok! But there aren't others function can instend of sprintf? If not,
> maybe we should think a way to pack sprintf. Otherwise, we will waste
> some space to make a ``infinitely long string''.

Actually, sprintf() doesn't really make any string (it doesn't allocate
any memory itself), but it assumes your string is as long as it needs
to be for the operation. sprintf() has no safety stops to catch the
case where the memory you gave it (in which to write the formatted
string, arg 1) is reaching its end. The memory used by both sprintf()
and snprintf() is the memory you have allocated before calling it.
snprintf() will just stop after n (arg 2) characters, usually the
length of your string - 1.

-- 
Shaw Terwilliger


This archive was generated by hypermail 1.03b2.