问题描述
我的理解是,printf的大多数实现依赖于像
my understanding is that most implementations of printf rely on something like
vsnprintf( _acBuffer[0], sizeof( _acBuffer[0] ), pcFormat, *ptArgList );
要真正处理的格式,然后他们输出他们通过放流。
to actually handle the formatting and then they output them to the stream via puts.
是否有减少_acBuffer的大小任何实现[0],同时保持打印所有字符串的能力要求?
Are there any implementation that minimize the size of _acBuffer[0] required while maintaining the ability to print all string?
显然是这样的:
printf("%s", pcReallyLongString);
将是一个问题。
您的想法是多AP preciated!
Your thoughts are much appreciated!
推荐答案
你的理解是错误的。我从来没有见过或printf 实现,首先格式化整个输出到一个临时字符串缓冲区工作听过的。通常printf的是周围的其他方法进行:基本构建块是
vfprintf
和 vsnprintf
是一个包装创造假文件
的缓冲区是目标字符串。
Your understanding is just wrong. I've never seen or heard of a printf
implementation that works by first formatting the entire output to a temporary string buffer. Usually printf is done the other way around: the fundamental building block is vfprintf
and vsnprintf
is a wrapper for that which creates a fake FILE
whose buffer is the destination string.
编辑:一些流行的(如的glibc)实现做在某种程度上利用无粘结大型中间缓冲区为某些格式,尤其是宽字符转换,并且将失败未predictably当他们无法为缓冲区分配足够的内存。这纯粹是一个低质量的实现问题,但是,有没有根本的原因任何的的printf
函数应该要求比工作空间,无论他们是什么样打印的小恒量了。
Some popular (e.g. glibc) implementations do make some use of unboundedly-large intermediate buffers for certain formats, especially wide character conversions, and will fail unpredictably when they cannot allocate sufficient memory for the buffer. This is purely a low-quality-implementation issue, however; there's no fundamental reason any of the printf
functions should require any more than a small constant amount of working space, regardless of what they're printing.
这篇关于无限缓冲的printf - 格式化看跌直接流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!