Thread: About std::flush

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    26

    About std::flush

    I see developers on web generally use:

    Code:
    std::cout << msg;
    but they sometimes use flush:

    Code:
    std::cout << msg << std::flush;
    What is the purpose of the std::flush? In which cases it is recommended to use?

    Cheers

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    It's recommended when you want your output to immediately appear on the screen, rather than whenever the stream chooses to flush on its own. One important application is in a multithreaded program, where multiple threads may write to standard output, and you want a given thread's output to appear together. You'd use a combination of locking and flushing the stream.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To expand a little, I/O is typically buffered because it's slow. Therefore, what you write may not immediately appear on screen.
    So in order to guarantee that you see what you want to write, you want to flush to stream. By doing this, you force whatever is stored in the buffer to be sent to the screen. Flushing is done by std::flush.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    It should be also pointed out that std::endl, which outputs newline character, also flushes the stream. (To avoid flushing when inserting a newline character, one has to explicitly insert '\n' instead of std::endl).

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Even if you insert '\n', there's no guarantee that the stream won't be flushed. std::flush and std::endl simply guarantees that the stream is flushed. Some implementations (e.g. MSVC) flushes the buffer when detecting a newline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flush?
    By mp252 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2013, 10:25 PM
  2. flush stdin
    By dezz101 in forum C Programming
    Replies: 7
    Last Post: 09-21-2008, 02:24 PM
  3. Flush or something....
    By Inquirer in forum Linux Programming
    Replies: 0
    Last Post: 04-16-2003, 07:55 PM
  4. Flush !!
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 07:44 PM
  5. flush()?
    By onurak in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2002, 09:35 AM