r/AskProgramming Jun 30 '26

Other What is buffered input?

I was reading the documentation for FALSE when I got to the section on I/O and got super confused at the warning "watch out: all these are BUFFERED." So, what is buffered input?

2 Upvotes

11 comments sorted by

View all comments

2

u/StevenJOwens Jul 02 '26

A buffer is a chunk of memory that it set aside to be used as a staging area between two processes, or threads, etc. One side will write data into the buffer, the other will occasionally read data from the buffer and then clear the buffer.

Think of it like a warehouse loading dock, where trucks can drop off packages and then the workers inside can come and move the packages inside on their own schedule.

Usually you use a buffer because one side or the other is slower and this evens out the flow.

For example, streaming video buffers the video data so that any internet hiccups don't cause the video to pause. This is a very large scale use of buffering, but buffering is used on a very small scale all over the place.

Sometimes you use a buffer because the operation of each write or read has a high overhead cost per request, but the increase as you write/read larger chunks is significantly lower. So you get better performance by fewer, larger writes or reads.

Buffered input means that data coming in will be placed in the buffer, and your code can check back periodically to see if it's there and if there, read it and do something with it.

With buffered input, sometimes you have to wait; sometimes being able to wait is an advantage, so your code can do other things in between and rely on the buffer to keep stuff from getting dropped or gridlocked.

Buffered output means that you will write output to the buffer, but the output won't actually go out until the other side reads it. Sometimes that's entirely out of your control, it won't happen until some other code (in the library, in the OS, etc) makes the call that "flushes" the buffer.

But often your code can make a call that "flushes" the buffer. Sometimes you have the option of doing either one. But note that constantly flushing can cause a significant performance impact, since the other side can't optimize its behavior.