Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

snprintf and snprintf_s

65 views
Skip to first unread message

jacob navia

unread,
Apr 26, 2014, 2:44:36 PM4/26/14
to
snprintf accepts a buffersize of zero and a null buffer pointer. That
means that snprintf is used to just calculate the number of characters
that would be required by the given format string and arguments.

Now snprintf_s however REQUIRES that the buffer is never NULL and that n
is always bigger than zero.

Why this discrepancy?

In general the _s functions are just the same functions as their
"normal" counterparts with extra checks. Why this difference here?

Or is it just a bug?
Message has been deleted
Message has been deleted

Bill Cunningham

unread,
Apr 26, 2014, 5:13:04 PM4/26/14
to

"jacob navia" <ja...@spamsink.net> wrote in message
news:ljguqh$bv5$1...@speranza.aioe.org...
http://www.lmgtfy.com/?q=n1570



Ben Bacarisse

unread,
Apr 26, 2014, 10:01:47 PM4/26/14
to
What is "this difference"? There are differences between snprintf and
snprintf_s, but the ability to calculate the number of characters
required to write the result is not one of them -- you just have to pass
something like

snprintf_s(&(char){0}, 1, ...)

rather than using a null pointer and zero.

<snip>
--
Ben.

jacob navia

unread,
Apr 27, 2014, 4:31:14 AM4/27/14
to
Le 27/04/2014 04:01, Ben Bacarisse a �crit :
Well that's precisely the difference!

I have to pass a dummy pointer and a dummy length to satisfy the
requirements. Why doesn't snprintf_s support the same interface as the
"normal" function?

The point is that you may be want to #define snprintf snprintf_s as you
may define fprintf as fprintf_s since the requirements are identical.

Now you have to memorize that the "_s" version of the function is NOT a
real replacement for the normal function and keep a different interface
for each one, a mess.


Ian Collins

unread,
Apr 27, 2014, 6:38:10 AM4/27/14
to
jacob navia wrote:
>
> I have to pass a dummy pointer and a dummy length to satisfy the
> requirements. Why doesn't snprintf_s support the same interface as the
> "normal" function?

Try asking Microsoft, they invented it!

--
Ian Collins

Ben Bacarisse

unread,
Apr 27, 2014, 8:12:23 AM4/27/14
to
jacob navia <ja...@spamsink.net> writes:

> Le 27/04/2014 04:01, Ben Bacarisse a écrit :
>> jacob navia <ja...@spamsink.net> writes:
>>
>>> snprintf accepts a buffersize of zero and a null buffer pointer. That
>>> means that snprintf is used to just calculate the number of characters
>>> that would be required by the given format string and arguments.
>>>
>>> Now snprintf_s however REQUIRES that the buffer is never NULL and that
>>> n is always bigger than zero.
>>>
>>> Why this discrepancy?
>>>
>>> In general the _s functions are just the same functions as their
>>> "normal" counterparts with extra checks. Why this difference here?
>>
>> What is "this difference"? There are differences between snprintf and
>> snprintf_s, but the ability to calculate the number of characters
>> required to write the result is not one of them -- you just have to pass
>> something like
>>
>> snprintf_s(&(char){0}, 1, ...)
>>
>> rather than using a null pointer and zero.
>>
>> <snip>
>
> Well that's precisely the difference!
>
> I have to pass a dummy pointer and a dummy length to satisfy the
> requirements. Why doesn't snprintf_s support the same interface as the
> "normal" function?

I can't say, but I was confused by your singling out of snprintf_s.
Many of the _s functions are quite different to the "normal" ones. In
that sense, snprintf_s does not stand out.

> The point is that you may be want to #define snprintf snprintf_s as
> you may define fprintf as fprintf_s since the requirements are
> identical.
>
> Now you have to memorize that the "_s" version of the function is NOT
> a real replacement for the normal function and keep a different
> interface for each one, a mess.

Yes, a mess. strcat_s is not a replacement for strcat. getenv_s is
very different to getenv, ctime_s can't be used as a macro replacement
for ctime... The list is huge. In fact, if fprint_s can be so used,
(the %n difference makes this unlikely) then it looks like the exception
rather than the rule.

--
Ben.
0 new messages