What does buffer flush means in C++ ? - GeeksforGeeks

08 Oct, 2021
A buffer flush is the transfer of computer data from a temporary storage area to the computer’s permanent memory. For instance, if we make any changes in a file, the changes we see on one computer screen are stored temporarily in a buffer. Usually, a temporary file comes into existence when we open any word document and is automatically destroyed when we close our main file. Thus when we save our work, the changes that we’ve made to our document since the last time we saved it are flushed from the buffer to permanent storage on the hard disk.
In C++, we can explicitly be flushed to force the buffer to be written. Generally, the std::endl function works the same by inserting a new-line character and flushes the stream. stdout/cout is line-buffered that is the output doesn’t get sent to the OS until you write a newline or explicitly flush the buffer. For instance,
C++
// Causes only one write to underlying file// instead of 5, which is much better for// performance.std::cout << a << " + " << b << " = " << std::endl;
But there is certain disadvantage something like, 
C++
// Below is C++ program#include <iostream>#include <thread>#include <chrono> using namespace std; int main(){  for (int i = 1; i <= 5; ++i)  {      cout << i << " ";      this_thread::sleep_for(chrono::seconds(1));  }  cout << endl;  return 0;}
The above program will output 1 2 3 4 5 at once.
Therefore in such cases, an additional “flush” function is used to ensure that the output gets displayed according to our requirements. For instance, 
C++
// C++ program to demonstrate the// use of flush function#include <iostream>#include <thread>#include <chrono>using namespace std;int main(){   for (int i = 1; i <= 5; ++i)   {      cout << i << " " << flush;      this_thread::sleep_for(chrono::seconds(1));   }   return 0;}
The above program will print the 
numbers(1 2 3 4 5) one by one rather than once. 
The reason is flush function flushed the output 
to the file/terminal instantly.
Note: 
You can’t run the program on an online compiler to see the difference, since they give output only when it terminates. Hence you need to run all the above programs in an offline compiler like GCC or clang.Reading cin flushes cout so we don’t need an explicit flush to do this.
You can’t run the program on an online compiler to see the difference, since they give output only when it terminates. Hence you need to run all the above programs in an offline compiler like GCC or clang.
Reading cin flushes cout so we don’t need an explicit flush to do this.
References:  
http://searchstorage.techtarget.com/definition/buffer-flush
https://stackoverflow.com/questions/15042849/what-does-flushing-the-buffer-mean
Related Article : Use of fflush(stdin) in C
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
PaneerParatha69
anujeetkunturkar12
tanwarsinghvaibhav
cpp-input-output
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Virtual Function in C++
Templates in C++ with Examples
Constructors in C++
Operator Overloading in C++
Socket Programming in C/C++
vector erase() and clear() in C++
Object Oriented Programming in C++