phobos / tango / ares

Kevin Bealer kevinbealer at gmail.com
Sun Feb 11 02:20:40 PST 2007


torhu wrote:
> Frits van Bommel wrote:
>> On a related note, one of the things that bothers /me/ is that no 
>> flush is performed at the end of the program. That causes some or all 
>> of the output to be missing if you don't explicitly flush after the 
>> last output.
>> I'd suggest adding the following to tango.io.Console:
>> ---
>> static ~this ()
>> {
>>          Cout.flush();
>>          Cerr.flush();
>> }
>> ---
>>
>> That would fix it, I think.
> 
> I considered this, but I'll try to explain why I didn't suggest it. 
> Basically, it hides that fact that you have forgotten to flush.
> 
> Pretend that this example is a big piece of code, that sometimes 
> crashes.  To debug, you print some numbers, and where the numbers stop 
> should be were the program crashes.
> 
> ---
> import tango.io.Stdout;
> import tango.stdc.stdlib;
> import tango.stdc.time;
> 
> void main()
> {
>     // some code here
> 
>     Stdout("1");
> 
>     // silly example of 'sometimes crash'
>     if (time(null) & 1)
>         free(cast(void*)1);
>     
>     Stdout("2");   
> 
>     // more code here, etc
> 
>     Stdout("3");
> }
> ---
> 
> The problem is that with auto flush at program end, it will print '123' 
> when it doesn't crash, and nothing at all when it does crash.  So the 
> flushing happens at the 'wrong' time, unless you're aware of the inner 
> workings of Tango.  A crash, exit by calling exit() or abort(), or an 
> uncaught exception will cause the auto flush not to happen.  Which seems 
> a bit inconsistent.
> 
> How realistic or valid this concern is, I'm not sure.  But it's at least 
> it made me not suggest auto flushing.
> 
> By the way, is there a particular reason why Cerr is buffered?

But forgetting to flush is a bug in user code *because* the system does 
not flush.  If the system flushed, then the user code is correct.

Put another way, this design decision (to not flush) helps programs that 
crash (the output is more deterministic), but it hurts programs that do 
not crash.  If a program would have been correct (on a system that 
flushes stdout), it now isn't because this system doesn't.

Kevin


More information about the Digitalmars-d-learn mailing list