Thread: endl and flushing the output buffer

  1. #1
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24

    endl and flushing the output buffer

    A little birdy told me that on some systems, if you do not use endl at the end of an output statement it will not do so until their are enough to make it worthwhile, anyone experience this and/or know of what systems this will occure on? Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    i am not able to get u. what do you mean to say. be specific. is that the endl fflushes the input buffer

  3. #3
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>anyone experience this and/or know of what systems this will occure on?
    I've seen it with C on FreeBSD and other Unixes, but C++ takes steps to avoid the issue, so you probably won't see it unless you're doing something out of the ordinary. Though it's always a good idea to prefer '\n' instead of endl unless you need to flush the stream. '\n' will never be less efficient than endl, but endl could potentially be expensive.
    Kampai!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    i am not able to get u. what do you mean to say. be specific. is that the endl fflushes the input buffer
    I don't get YOU. Please refrain from abbreviating 3-letter words. Even if you're learning English as a second language, you could help yourself out a lot by just capitalizing the first letter of every sentence and typing properly.

    A little birdy told me that on some systems, if you do not use endl at the end of an output statement it will not do so until their are enough to make it worthwhile, anyone experience this and/or know of what systems this will occure on? Thanks.
    Yeah, I've seen that, but it's implementation specific as Sake pointed out, so keep it in mind if you intend your program to be used on multiple systems... Other than that I'll just second what Sake said - good advice.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, there is the flush() member function of ostream that you can use to flush the buffer as well. And, to be honest, if you are trying you optimize your program by using '\n' instead of std::endl, then there is a fair chance that you are optimizing the wrong thing (not that there aren't reasons/situations calling for '\n' instead, but it is really probably not the source of great slowdown in most programs).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > anyone experience this and/or know of what systems this will occure on?
    Well if you start relying on non-standard (or undefined) behaviour, then simply downloading the latest patch to your compiler could break all your programs.
    Nevermind the problems which would occur porting your code to another platform.

    If you're just outputting a large block of text (say a help message), then you could easily replace all the endl with '\n' for all the lines except the last one, and it would not matter.
    But if you're in the middle of a series of prompts and responses, then endl could be vital in making the next prompt visible to the user.

    But as Zach points out, this is hardly the most appropriate substitution for performance reasons.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I've seen it with C on FreeBSD and other Unixes
    Shouldn't that be "Unices"? (like 'index' -> 'indeces')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endl or \n ?
    By plain in forum C++ Programming
    Replies: 5
    Last Post: 09-01-2006, 01:50 PM
  2. question about endl
    By hern in forum C++ Programming
    Replies: 7
    Last Post: 04-15-2004, 01:49 PM