setvbuf函数_IONBF模式下size参数含义及缓冲区、传0合法性咨询
setvbuf() with _IONBF (Unbuffered Mode) Let's break down your questions clearly, as this is a common point of confusion with stream buffering in C:
1. What's the meaning of the size parameter for unbuffered streams?
When a stream is set to unbuffered mode (_IONBF), the size parameter has no defined meaning per the C standard. Unbuffered streams operate by sending every read/write operation directly to the underlying OS immediately—there's no intermediate buffer to accumulate data, so a "buffer size" doesn't apply here. Most standard-compliant implementations will simply ignore the size argument entirely in this mode.
2. What's the significance of size when mode is _IONBF? Does it allocate a buffer?
Short answer: none, and no.
When you specify _IONBF, the C library does not allocate any buffer for the stream, regardless of what value you pass for size. The parameter is effectively ignored because unbuffered mode bypasses buffer-based I/O entirely. Even if you pass a non-zero size, the library won't use it to allocate or manage a buffer—your I/O calls will still interact directly with the file descriptor every time.
3. Is passing 0 for size legal when using _IONBF?
Absolutely legal. Since size is ignored in _IONBF mode, any value (including 0) is acceptable. In fact, passing 0 is a common practice to make your code intent clear: you're explicitly signaling that you don't need a buffer, so you're providing a placeholder value that won't be used.
A typical, idiomatic call to set a stream to unbuffered mode looks like this:
setvbuf(stdout, NULL, _IONBF, 0);
内容的提问来源于stack exchange,提问作者chqrlie

