I'm wondering what sort of side effects there are for using the setvbuf
function.
An example use case:
setvbuf(stdout, NULL, _IOFBF, BUFSIZ); // Call #1
// Many calls to fprintf(stdout, ...);
setvbuf(stdout, NULL, _IONBF, BUFSIZ); // Call #2
// Many calls to fprintf(stdout, ...);
setvbuf(stdout, NULL, _IOFBF, BUFSIZ); // Call #3
// More calls to fprintf(stdout, ...);
Given that Buffer A is allocated with the calls to printf
after Call #1, I would want one of two behaviors:
printf
after Call #2printf
after Call #3What I don't want to happen would be for Buffer A to not be freed or reallocated, giving me a memory leak.
Is the actual behavior one of the two desired behaviors?
The C standard specifies (7.19.5.6):
The setvbuf function may be used only after the stream pointed to by stream has been associated with an open file and before any other operation (other than an unsuccessful call to setvbuf) is performed on the stream.
So repeatedly calling setvbuf
on the same stream, as you do, is undefined behavior.
You didn't specify what platform you were on, but glibc's implementation doesn't seem to reallocate the memory.