0

I think this is a duplicate question, but i didnt understand that from the answers. My question is what is buffer and what does flushing buffer mean in C++. So I think buffer is a storage of data where all datas stores and cleaning buffer forces to print the result or something, but if I correct then why would i dont clean buffer, what is the necessity of it why there is \n instead of only endl? this is my question. Does it saves ram or something? So my actual question is what is buffer and flushing buffer, and if I am guessing correct then what is the necessity of not Flushing buffer sometimes.I am beginner so hard code example is not understand able for me.

  • what buffer? Duplicate of which question? Asking a follow up question can be ok, but then you should add a link to the original question. – 463035818_is_not_an_ai Aug 07 '21 at 13:15
  • are you talking about `std::cin` and `std::cout`s io buffers? – 463035818_is_not_an_ai Aug 07 '21 at 13:16
  • I am talking about both –  Aug 07 '21 at 13:19
  • This is the link ```https://stackoverflow.com/questions/5112992/why-is-buffering-in-c-important``` –  Aug 07 '21 at 13:21
  • did you read all answers to that question? It is not clear what you want to know that is not covered by them – 463035818_is_not_an_ai Aug 07 '21 at 13:23
  • Waiting to flush the buffer isn't about saving RAM, it's about saving time. Flushing the buffer does mean printing to console, or printing to file, or some other kind of input/output operation. It is often _much slower_ to actually perform the system calls to print something than it is to just save it in a "print this later" buffer. It also lets the stream amalgamate those system calls. In the event that there's a fixed overhead for each system call to print, a few flushes that each send a bunch of characters will take less time overall than many small flushes. – Nathan Pierson Aug 07 '21 at 13:42
  • my main problem is if flushing buffer makes program running fast, then why some ppl dont flush it for example they use \n instead of endl. What is the necessity of not flushing buffer –  Aug 07 '21 at 14:12
  • @rafid Many people simply don't know the difference, because every single tutorial teaches to use `endl` to avoid questions like "I don't see any output, why?????". If you flush often, your program gets quite significantly slower, but this cannot be noticed on simple toy programs when you learn programming - humans are orders of magnitude slower than computers in processing external inputs. – Yksisarvinen Aug 07 '21 at 14:57
  • "my main problem is if flushing buffer makes program running fast" it doesnt. It is not flushing the buffer that is fast, but not flushing the buffer after each output. – 463035818_is_not_an_ai Aug 07 '21 at 15:38
  • So, are you guys saying if i input or output something then it will store in buffer? And after running it buffer will be cleared, but endl force buffer to run to flush or clean the buffer? And that's why cout doesn't output sometimes bc buffer couldnt be flushed? And that's why ppl use endl to force the buffer to run it, but too much cleaning will make the program slow? Am i correct, and did i understand this thing? –  Aug 07 '21 at 16:47
  • in simple terms, "flushing the stream" means to actually show the output on your screen. This does not necessarily happen when you call `std::cout << "something";`, and yes printing from the buffer to the screen takes time. This is explained in this answer: https://stackoverflow.com/a/5115214/4117728. The time is not proportional to the number of character, but each printing on the screen is expensive, so you dont want to do it for each single character – 463035818_is_not_an_ai Aug 07 '21 at 16:56
  • so am i correct? And also how to know when should i clean buffer and when not, and what problems occur when i don't clean buffer, I mean why it doesn't output? how can i understand that it will not print bc of not cleaning buffer and this will print if i dont clean buffer –  Aug 07 '21 at 17:12

0 Answers0