What exactly is flushing?
Asked Answered
J

3

12

I'm brand new to coding and programming (started today actually). I've been watching a few videos and reading the beginning to a few books to see which I can click with, but I'm having trouble understanding some of it.

One of the videos utilized endl and another utilized \n in the basic "Hello World" introduction. I wanted to know more about the differences between the two.

So, I understand that the difference between endl and \n is that endl will flush the code while \n will not, which makes endl slower. That much I can search for on Google.

However, when searching for flushing C++, I'm not able to make heads or tails of what it means because of the jargon.

What does it mean to flush, and when and why would you want to do it? What is a buffer?

Please explain it in a very basic way, if you can.

Jone answered 10/12, 2017 at 4:26 Comment(5)
Does the explanation here help? Your question is a bit broad to be answered concisely at Stack Overflow.Chloramphenicol
Think about it literally. You let run in some water to your bathtub up to a certain amount (bathtub is the buffer), and with flush() you pull the plug.Chloramphenicol
@Chloramphenicol - a more literal thought process would use a toilet. The reservoir (buffer) fills up, and flushing drains it by permitting water in the reservoir to move to the toilet bowl.Maize
@Maize I preferred to choose a more neutral example, but yes.Chloramphenicol
@Chloramphenicol - the problem is "pulling the plug" is a neutral action, whereas flushing is active, so your description is not of flushing. The word "flush" describes an action of providing an impetus that directly causes the buffer to empty, or the water to move, whereas "pulling the plug" describes another action that has a side-effect of indirectly permitting the buffer to empty (or the water to move).Maize
B
10

Buffers are temporary memory used to store the input of a process that can take some time.

It can be either for not losing any data. Think of a communication between a fast computer and a slow one. The fast computer may send bytes at a higher rate than what the slow one can handle, while the slow one is processing a byte, others are then still arriving and they are stored in a buffer waiting to be processed.

Or, it can either be for performance reason. All operations have a static cost of time that is independent of the size of the data processed by the operation (it may need to initialize some data, wait for resource, ...). If that cost is not negligible, it could be interesting to pay that cost the least times possible. So we use a buffer to gather more data and merge multiple operations into one.

For your case, printing to the screen has not a negligible static time and it is why a buffer is used by default and the data is only printed if enough bytes are received.

Flushing a buffer means emptying it and forcing to treat the data in it. It can be useful if you want to immediately treat small amount of data.

For example, if you want to print "Please enter your age:" and wait for the user to enter a number, it's preferable that the user actually sees that request ! So here, flushing the buffer is mandatory.

But if you print a stream of texts (think of a program printing many texts like a compiler when compiling a big project) it's better to not flush and let the buffer fill up so that you don't spend the static time for each block you send (each source file for the compiler case for example). And flush after the last block to be sure we do not end up in the middle of the buffer, which is very likely.

Benefic answered 10/12, 2017 at 5:24 Comment(0)
B
7

@Nicolas Dusart gave a great answer. Here is the same information but explained from a different context.


"Printing to the screen" can mean 2 things:

  1. EVENTUALLY printing to the screen. lazy evaluation is not a perfect analogy, but the theory is the same.
  2. IMMEDIATELY printing to the screen.

Now, when you call "print", the system intelligently interprets that as eventually. You can ask it to immediately print to the screen, however.

Why is this confusing behavior intelligent? Well, imagine you have all your Aunties over, and you want to serve them tea.

  • When the first one asks for a cup, you have a choice of immediately running to the kitchen and making her a cup, or eventually running to the kitchen and making her a cup.
  • When the last one asks for a cup, you have a choice of immediately running to the kitchen and making her a cup, or eventually running to the kitchen and making her a cup.

Despite the two questions being almost exactly the same, it makes sense to answer them completely differently. When making the tea, you only want to put water to boil once. And you only want to run to the kitchen once. Despite the fact that you want to hand out a cup of tea 3 times. (I have 3 aunts lol)

Now, because we aren't talking about running to the kitchen and fulfilling a remembered request, and instead we are talking about dealing with data in a buffer, we do not call this action remembering a request and immediately running to the kitchen, but instead we call it buffering and flushing.


So to answer your question explicitly (and to repeat exactly as our good friend said): "flush" means "immediately deal with data in a buffer". It is synonymous with "clear a buffer of its data".

The difference between these two meanings really blurs when we consider the case of buffering a request to fulfill later. As an engineer, I think that either meaning is a valid use of the term. As a computer science student, flush only means "clear a buffer" to me.

Brasca answered 22/3, 2021 at 17:21 Comment(0)
B
0

To flush, means to empty the buffer.

Now, buffer is temporary storage area for storing data.

Both endl and \n can be used to print newline character but there is minor difference between these two :

In case of endl, buffer is cleared by usage of internal call to flush the buffer

In case of \n, newline is printed but buffer is not flushed as no extra call is made

So performance-wise, endl is better than \n

Bytom answered 10/12, 2017 at 4:47 Comment(1)
Performance-wise, endl is actually worse than \n. Flushing means we are shortcutting the buffer and buffer are here for performance reason, so no, endl is not used for performance issues.Benefic

© 2022 - 2024 — McMap. All rights reserved.