Re: A small game
On Sep 17, 5:56 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze wrote:
The usual reason for flushing an ostream (not just cout, any
ostream) is to ensure that the data has been output.
But the proper way to ensure that is to use std::flush, not
std::endl (which, ie. the newline, might not even be the last
thing you wrote to the output stream).
There's a long tradition for using line buffering, which is what
std::endl is meant to emulate.
Also the need to explicitly flush an output stream is quite rare.
That's simply not true. Except for small, Unix type filter
programs, I don't think I've ever output where there wasn't a
need to flush. It's the rule, rather than the exception.
The only situation where you must do it is when you open a
file, write something to it, and then *keep it open* while the
program does other things (and want to make sure that what you
wrote is flushed to the file rather the residing on an output
buffer). While there are practical cases where you need to
keep a file open during the execution of a program, the more
usual situation is that you open the file, write data to it
and then close it (or simply let the ofstream instance go out
of scope), which will effectively flush it.
Again, that doesn't seem to correspond to my experience. Maybe
in client programs? (But then, wouldn't they generally be
writing to some sort of database?)
The biggest problem with std::endl is precisely that it
flushes the output stream.
And how is that a real problem? It's purely a performance
issue, and doesn't affect most programs at all.
Because it's a BIG performance issue. We are talking in the
worst case about an order of magnitude slowdown. It is a very
important issue if you are writing lots of data to a file.
And I repeat: I use endl most of the tie, I write lots of data
from time to time, and it has never been a performance problem.
If a new user is taught that std::endl is good and should
always be used by default, he will learn that bad habit,
If a new user is taught to only use '\n'
I never said that he should be taught to *only* use that. I
said that he should be taught both, and why use one or the
You can't teach everything at once. Before he's through, yes,
he should know both, and know how file buffering works. But
still, the default is std::endl.
and it will carry on a long time. At some point he will start
creating programs which write enormous amounts of (text) data
to an output file, and by custom he will use std::endl to
write newlines to it. If these newlines are very common in
this output, in most systems the writing will be slowed down
by a rather big factor.
That sounds like vacuous speculation to me.
I wouldn't have written that if I didn't have personal
Which disagrees with my experience.
IMO the difference between "\n" and std::endl can and should
be taught from the very beginning, and the recommendation
given that "\n" should be used by default.
And that's just wrong. Experienced programmers use endl by
default, and most of the time, don't even worry about the
I suppose you are calling me an inexperienced programmer then.
(Yes, I *do* have actual literal experience of the speed
differences between writing std::endl and "\n".)
I know that the difference exists, but I've never encountered a
case where it was significant. At any rate, it's a performance
issue, only to be addressed once the program actually works.
For beginners, it is absolutely essential that what they've
written be output, so that they don't get confused when
debugging. And even for experienced programmers, if the program
crashes, it's a lot easier to figure out what went wrong if the
data you wrote actually made it to the disk.
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34