调用 malloc 的 snprintf,或不调用 malloc 的 snprintf [英] snprintf that calls malloc, or snprintf that does not call malloc
问题描述
我需要保证从不调用 malloc 的 snprint 变体.
那是因为这个 snprintf(我们称之为 safe_snprint())将要
从 malloc 失败或死锁的地方调用.
I need variant of snprint that has guarantee that it never calls malloc.
That's because this snprintf (let's call it safe_snprint()) is going to
be called from places where malloc will fail or deadlock.
什么更接近真相,1 还是 2?
What is closer to truth, 1 or 2 ?
在 Windows 上,本机 snprintf 可能会调用 malloc.然后
我需要拉开源 snprintf.c 并将其称为 safe_snprintf().或者
On Windows, native snprintf might call malloc. Then
i need to pull opensource snprintf.c and call it safe_snprintf(). Or
在 Windows 上,本机 snprint 保证永远不会调用 malloc.
On Windows native snprint is guaranteed to never call malloc.
如果在某处有记录,我更喜欢 (2).谢谢
I'd prefer (2) if it is documented somewhere. Thanks
推荐答案
VC 库 (MSVCRT) 中 _snprintf()
的实现只会在浮点转换时调用 malloc 当且仅当 (iff) 格式精度超过 163 个字符.这适用于 %E
%G
%A
%e
%f
%g
和 %a
格式说明符.
The implementation of _snprintf()
in the VC libs (MSVCRT) will only call malloc on floating point conversions if and only if (iff) the format precision exceeds 163 characters. This applies to %E
%G
%A
%e
%f
%g
and %a
format specifiers.
这应该适用于至少从版本 6 开始的所有 MSVCRT 版本.
This should apply to all releases of MSVCRT since at least version 6.
这篇关于调用 malloc 的 snprintf,或不调用 malloc 的 snprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!