So, I delved into how the libd lib part is implemented, and I came across another question. Looking at man setvbuf , I see the following:
When the first I / O operation occurs, the file, malloc (3), and the buffer are called.
That makes sense, your program should not have malloc in it for I / O unless you actually use it. My reaction to this is that libc will clear its own mess here. I can only assume that this is because valgrind does not report a memory leak (they could, of course, do something dirty and not distribute it directly through malloc ... but we will assume that it currently uses malloc )
But , you can also specify your own buffer ...
int main() { char *p = malloc(100); setvbuf(stdio, p, _IOFBF, 100); puts("hello world"); }
No memory leak! valgrind confirms this. Therefore, it seems that whenever stdio independently allocates a buffer, it is automatically deleted (no later than when exiting the program, but it may close in the stream). But if you explicitly specify a buffer, you must clear it yourself.
However, there is a trick. The man page also says the following:
You must make sure that the space, which by the time moment the thread is closed, which also occurs when the program ends. For example, the following is not valid:
Now it becomes interesting for standard streams. How to properly clear the manually allocated buffer for them, since they are closed at the end of the program? I could imagine โclearing this when I close the flagโ inside the file structure, but it gets hairy, because if I read it correctly, we will do something like this:
setvbuf(stdout, 0, _IOFBF, 0); printf("hello "); setvbuf(stdout, 0, _IOLBF, 0); printf("world\n");
will result in 2 allocations of the standard library due to this sentence:
If buf is NULL, only the mode is affected; a new buffer will be allocated on the next read or write operation.
EDIT: Adding to my question. Since it is clear that I should pass any buffers to setvbuf , if I really use it on stdout , is there any practical way to free it? He must live to complete the program. The best I can come up with is fclose(stdout) , then free it up or use the static buffer that some people talked about. I ask because it seems like an awkward design decision.