Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

endl and flushing the buffer

Tags:

c++

flush

buffer

In the C++ primer book, in chapter (1), it mentions the following:

endl is a special value, called a manipulator, that when written to an output stream has the effect of writing a newline to the output and flushing the buffer associated with that device. By flushing the buffer, we ensure that the user will see the output written to the stream immediately.

What is meant by "flushing the buffer" here?

like image 460
Simplicity Avatar asked Jan 20 '11 20:01

Simplicity


People also ask

What does flushing a buffer mean?

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.

How do you flush a buffer in C++?

Using “ fflush(stdin) ”: Typing “fflush(stdin)” after “scanf()” statement, also clears the input buffer but generally it's use is avoided and is termed to be “undefined” for input stream as per the C++11 standards.

Why would you have to flush a buffer?

The buffer flush is used to transfer of computer data from one temporary storage area to computers permanent memory. If we change anything in some file, the changes we see on the screen are stored temporarily in a buffer. In C++, we can explicitly have flushed to force the buffer to be written.

What Endl means?

Standard end line (endl) It is used to insert a new line characters and flushes the stream.


2 Answers

Output is generally buffered before it's written to the intended device. That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character.

Flushing means emptying the buffer and actually writing it to the device.

like image 89
Benjamin Lindley Avatar answered Sep 20 '22 11:09

Benjamin Lindley


C++'s iostreams are buffered, that means that when you output to an ostream, the content will not immediately go to what is behind the stream, e.g. stdout in the case of cout. The implementation of the stream determines when to actually send the buffered part of the stream out. This is done for reasons of efficiency, it would be very inefficient to write to a network or disk stream byte by byte, by buffering this problem is solved.

This does however mean that when you write say debug messages to a log file and your program crashes you may lose part of the data you wrote to the log file through the stream, as a part of the log may still be in the stream's buffer and not yet written to the actual file. To prevent this from happening you need to make the stream flush its buffers either by an explicit flush method call, or by using the convenience of endl.

If however you're just writing to a file regularly you should use \n instead of endl to prevent the stream from unnecessarily flushing the stream every line reducing your performance.

Edited to include this note:

cin and cout have a special relationship, where reading from cin will automatically flush cout beforehand. This makes sure that the e.g. the prompt you wrote to cout will actually be seen by the user before the read from cin is waiting for input. Hence, even in cout you don't normally need endl but can use \n instead. You can create such relationships between other streams as well by tying them together.

like image 43
wich Avatar answered Sep 21 '22 11:09

wich