[Courses] Re: [C] A clarification on fflush(stderr)

Malcolm-Rannirl rannirl-lc at otherkin.net
Wed Aug 7 11:52:15 EST 2002


On Wednesday 07 August 2002 02:31 am, Anand R wrote:

> 	Just a small doubt. I have seen many flushing stderr in their code.
> Why is it necessary to flush stderr?? A small code example below:

One I can actually answer. :)

When you write to stderr (or any other file), the output is buffered by the 
OS, because it's not very efficient to write everything to disk immediately. 
Unfortunately, if your program (or the OS, as I've had happen with Windows) 
crashes then those buffers die with it and anything in them is lost. 

Which means if you wrote an error message just before crashing, it may be 
lost. Depending on the write delay, so might previous messages, sometimes 
buffers only get flushed when they hit 1k boundaries or similar. (That tends 
to be system dependent).

Flushing stderr (or whichever file) forces the buffers to write immediately 
rather than waiting, thus preserving your error messages and hopefully giving 
you a better idea what went wrong. This is especially useful in debug code 
such as the example you gave.

Having had to debug Windows apps that would take out the entire machine 
without so much as a BSOD, the force-to-file ability can be essential. (I'd 
also have done almost anything for a core dump in that situation, but that's 
something Windows has never provided. Thankfully I work on linux boxen these 
days).



-- 
Be careful what you want.
It might want you.
- 'the Dragon' Elvendrums

<<------------------------------------------------------------------>>
<<  This email is monitored by the US government under the auspice  >>
<< of the USA act. For private communication, ask me for my PGP key >>
<< ----- PGP: envelopes for email - www.pgp.com www.gnupg.org ----- >>



More information about the Courses mailing list