No, there's no guarantee that it won't. However, most implementations I've seen tend to use a fixed size buffer for creating the formatted output string (a).
In terms of glibc (source here), there are calls to malloc
within stdio-common/vfprintf.c
, which a lot of the printf
family use at the lower end, so I wouldn't rely on it if I were you. Even the string-buffer output calls like sprintf
, which you may think wouldn't need it, seem to resolve down to that call, after setting up some tricky FILE
-like string handles - see libio/iovsprintf.c
.
My advice is to then write your own code for doing the output so as to ensure no memory allocations are done under the hood (and hope, of course, that write
itself doesn't do this (unlikelier than *printf
doing it)). Since you're probably not going to be outputting much converted stuff anyway (probably just "Dang, I done run outta memory!"
), the need for formatted output should be questionable anyway.
(a) The C99 environmental considerations gives an indication that (at least) some early implementations had a buffering limit. From my memory of the Turbo C stuff, I thought 4K was about the limit and indeed, C99 states (in 7.19.6.1 fprintf
):
The number of characters that can be produced by any single conversion shall be at least
4095.
(the mandate for C89 was to codify existing practice, not create a new language, and that's one reason why some of these mimimum maxima were put in the standard - they were carried forward to later iterations of the standard).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…