简体   繁体   中英

C - implications of fflush(stdout)

Does fflush(stdout) do anything besides flushing the output buffer?

Or what does flushing the output buffer imply?

Because in a scheduler, I just resolved a segfault by throwing an fflush(stdout) into the context switch, even though for debugging purposes, all writes to stdout had been disabled, which - as far as I'm concerned - should have rendered any kind of flushing obsolete.

For output streams, fflush() with non-null argument writes any unwritten data from the stream's buffer to the associated output device. How does it do this is implementation dependent.

Because in a scheduler, I just resolved a segfault by throwing an fflush(stdout) into the context switch

You can close stdout explicitly at the start of program to verify your findings. Good chances are that the problem lies somewhere else or the implementation of stream on your system is buggy.

Without seeing actual code, we can only speculate.

However, adding more code to a function can have an indirect effect of changing memory layout in your program. The nature of the change - if any - depends on what the function does (does it allocate memory, declare lots of variables, etc), how the operating system manages the executable code in memory while running it, etc.

Odds are, somewhere else in your code, there is an invalid operation (invalid operation with a pointer, etc) and the effect of the additional statement is simply changing the symptom, by changing what your program is doing with the affected memory.

I suppose it is remotely possible that there is a bug in fflush() . But I wouldn't bet on it - standard I/O functions like fflush() are used by a lot of people and bugs in such function in a library that has existed for a while (eg from a vendor that has released several versions) are likely to have historically been found, reported, and fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM