endl or \n ?
I have searched quite a few FAQ`s, C/C++ Reference site and the FAQ`s here and I`m still sort of lost about endl and \n. I know that both are used to end a line. That \n is quicker. That (from what some suggest) endl flushes the buffer and sticks a \n at the end of the line so as to start a new one. However, I can`t grasp flushing the buffer. I know it is important to keep from hogging up memory but I can`t get my mind around the buffer and its importance in helping memory or giving extra space to the memory (as I think the term suggests) in case something doesn`t fit like it should.
Can someone lay it out for me?
And having answered that question could you tell me generally, the circumstances under which I should be posting endl to my code as opposed to \n?
Thanks All, ..Plain
Number 6 is a good reference as to when one should use endl
Use of a buffer speeds things up because it is generally a slow operation to send data to a console window, or file, or any other output device. Instead of sending one character at a time, the characters are stored up in a buffer and sent as a group. Flushing the buffer merely ensures that anything you are attempting to write to your output device is actually written instead of being saved for later. It really isn't about memory savings, it is about not running an expensive operation more than necessary.
>Can someone lay it out for me?
It's faster to copy to an array in memory than to read or write from a physical device. The point of a buffer is to avoid the expensive read or write until the last possible moment (ie. the user wants to flush the buffer, or the buffer becomes full). That's all there is to it.
>could you tell me generally, the circumstances under which I should be posting endl to my code as opposed to \n?
Use endl when you don't expect the buffer to be flushed automatically and need it to be. User prompts don't count most of the time because you're going to request input anyway and cin is tied to cout; any request for input flushes the tied output buffer. Error messages don't count as long as you use cerr, which is unit buffered and flushes automatically every time.
endl is typically most useful with non-user viewable streams, such as a file stream. One example would be error handling. You're doing some kind of processing and then writing to a file stream. An error occurs in the processing that requires termination. This is a case where you should flush the buffer before terminating. Though in that case an explicit call to flush would be better than endl unless you need to write a newline for some reason. Another example would be a protected write situation. You know the write will work, but the next operation, while unrelated, is highly suspect for causing program meltdown. It would be wisest to make sure that the write succeeds and the buffer is flushed, just in case.
In my experience, endl is rarely needed and terribly overused.
Thank you all. This has helped quite a bit. I appreciate the time you took to reply.
In the case of protected write (usually used for log files), it's more comfortable to just set the unitbuf flag and have the stream flush itself after every operation.