+ 1
What's flush ?
In c++ I read in a lesson that cin flushes input What does this mean? 🤔
6 odpowiedzi
+ 7
Input (and output) streams have a small reserved space in memory that is termed a "buffer". These buffers help prevent data loss in case the stream data flows in faster than it flows out (or vice versa). Suppose a person pastes a paragraph as input to the console. The incoming characters are quickly stored in the input buffer and there they await for the program to read them, say, via cin. Your program might need only some portion of the input and you are willing to ignore the rest that is in the buffer. That is when you would do a flush of the input stream so that it empties the remaining characters out from the buffer without passing them to your program. The next time your program gets input, it would get whatever was typed after the flush.
A flush on the output stream, as performed by cout << endl, is a little different. In this case endl forces whatever is currently in the partial buffer to get written out, instead of waiting for the buffer to fill up before writing as it normally does.
+ 2
It has been a long time since I have had to deal with input arriving faster than the process could consume it. Computers were slower back then. There was a concept of double-buffering that was supported by the operating system. Usually that handled it. If that was not enough, then the program received a fault code and had to accept the fact that data were lost due to buffer overrun. Then it was time to get more creative about faster data handling and storage - whether it was through compression, more memory, or faster disks, communication protocol - something had to be done better.
-----
There is no separate "partial buffer". I meant the output buffer gets written out, even though it is only partially filled.
+ 1
Brian
1- To learn more about those factors that affect or limit the code execution whether they are hardware components or techniques such as what you said communication protocols or data handling, what do you recommend to me to do?
2- how it is done to maintain the order of the output steam?
For example:
_______________________________
| 1 | 5 | 0 | 7 | 8 | 2 | 23 | 12 | 234|
----------------------------------------------------
↑(flush)
______________________________
| | | | | | 2 | 23 | 12 | 234|
--------------------------------------------------
Now new input stream till stream is filled
_______________________________
| A | b | c | d | e | 2 | 23 | 12 | 234|
----------------------------------------------------
So the buffer is filled then the output will be :
A b c d e 2 23 12 234
While it should be:
2 23 12 234 A b c d e
To maintain the order of displaying
Or do the buffer shifts the remaining characters to the beging then receives after them till the end
+ 1
Ogu Joseph
I have no idea how much this is going to help me with my question ⁉️
https://code.sololearn.com/cE7hl29V657r/?ref=app
0
Brian
What I understood is as following:
1- there's a reserved memory called buffer to compensate the difference in flow between input and output streams flow.
Q)==> what can we do if the buffer is filled and still need it wait more and take more characters then output them all together? In other words, I need the output occurs when all input ends even if it's bigger than the buffer capacity.
2- endl will output the previous characters stored in the buffer and store the rest of the buffer in the some memory allocation called partial buffer then get transferred to the buffer again then the buffer starts to receive more characters to output them all when the buffer is filled except for facing another endl that flushes input
3- which is better or what are the benefits of both to determine the optimal implementing circumstances?
Note)==> of course, if I misunderstood anything, correct me
0
1. To study each area would be a lot of branching out into different topics. Pragmatically, I would suggest to start by determining where the data flow restriction is, and then research or invent ways to solve it while bearing in mind the economics and politics to implement and maintain each idea. That's all part of the engineering mindset.
2. Streams in general can be modeled as queues - first in, first out. An output stream can be pretty simple. It may reset the pointer to its beginning after a flush. An input stream might be implemented as a ring buffer - it just keeps advancing and wraps around to the beginning when it reaches the end. The addressing resembles a modulo operation, returning to the "zero" point when it reaches the modulus value.