What does Buffer Flush mean in C++
A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either on a windows mac OS or even a Linux variable environment. To explain it further, we will consider the scenario where we made some code changes in a particular industry file we are working on. Then the changes we have made will be stored as discussed in the rentable temporary storage in a buffer space.
For loop in C++ (To help us understand the Buffer and Flush use cases)
Code
#include <iostream>
using namespace std;
int main() {
for(int i=1;i<=10;i++){
cout<<i <<"\n";
}
}
Output
1 2 3 4 5 6 7 8 9 10
To give us a context of the rentable temporary, let us say that we are working on a Microsoft word document, and it happened to us like this while the work is temporarily on hold or even after finishing the work, the main file has been closed which makes the work file we have created will also be destroyed. Thus we are advised by the products of Microsoft like word, power point, and excel products while closing the file after finishing the work prompts us to save the job. When we keep our work on a product like a notepad or a word the last time, we hold the document or file and are flushed from the tempo buffer to the permanent storage environment on our hard disk built-in.
In C++ programming language, we have the flexibility to explicitly be flushed to do a force acting to the Buffer to be written. The usual function, std::endl, has the code functionality that works like the same by applying the simple technique of inserting a new line character and then flushing it to the stream. stdout/cout is a line-buffered that it has been OS that it to be the output which doesn't get sent to the OS until a person the user writes a new line or perform an explicitly flush it into the Buffer
Below is the code to demonstrate Buffer along with its syntax
Syntax
//features like it Cause only to have one write to an underlying file in the program
//we are having instead of 30, which is much better for
// performance testing rather choose some lesser variable count to check the buffer flash in C++
std::cout << a << " + " << b << " = " << std::endl;
Code
//here, we are having the below C++ program to demonstrate
//the concept of buffer flush concept in detail
// we have also included the library files like thread, Chrono
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
for (int i = 1; i <= 30; ++i)
{
cout << i << " ";
//below driver code is most important as it drives the buffer flush
//without the below code, it is just a normal loop program printing
// its output
this_thread::sleep_for(chrono::seconds(2));
}
cout << endl;
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
The above code will give the output at once, taking some time.
Flush Function
The advantage of the Flush function is that it takes time and slowly starts outputting the numbers in the loop until the base condition is met.
- We cannot run the code on any of the internet-available online compilers to see the difference ourselves since they have output-only terminals which are run by the backend Linux operating system. to run and understand all the programs, we can make use of the offline compilers after downloading them from the popular internet ones being CLion, Dev C++, Microsoft Visual studio code etc.
- Reading cin flushes cout, so we don't need an explicit flush to do this.
Code
// 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;
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30