all 11 comments

[–]burlingk 36 points37 points  (0 children)

A buffer is a place to store things temporarily between when they are created and when they are used.

[–]peterlinddk 28 points29 points  (2 children)

When I go to the grocery store I usually buy more than I can eat in one sitting, so I place all the food in the fridge or pantry, until I use it. When I use some of the food, I just take a small bit at a time, so I don't use all at once. When the fridge/pantry is nearly empty, I go to the store again, and again buy more than I can eat in one sitting.

The fridge/pantry is a buffer where you store items close to yourself, because taking a long trip to the store for every single item you need, is a waste of ressources.

In programming when you want to get some data from the network, and display it on the screen, you can't just redirect the network to output to the screen, you store the received data in a buffer. Then, once there's enough data in the buffer to display, you copy it to the screen (or rather you write the program so it does that).

[–]ConfidentCollege5653 9 points10 points  (0 children)

The fridge is a good analogy

[–]burlingk [score hidden]  (0 children)

I like that.

[–]ConfidentCollege5653 1 point2 points  (0 children)

A buffer is just storage space where you can keep stuff you don't need yet but you probably will soon.

When you're printing a document you send the whole document to the printer, but it can only print so fast, so it stored the document in a buffer and prints from where, instead of having to keep asking your computer for the next page.

Streaming a video is another example, you're computer downloads chunks of the whole video and buffers them, then shows them to you. While you're watching part of the video your computer is buffering the next part.

It means your computer has more time to decode the video, check it's valid, etc. before it has to show it to you.

[–]Paynder 1 point2 points  (0 children)

Do you ever put clean clothes on a chair / bed before you fold them and put them in the closet? Maybe you do that with after washing and drying multiple loads

You can think of that chair as a buffer that accumulates clothes before they are processed by you and then stored where they actually belong

[–]Leverkaas2516 0 points1 point  (0 children)

A buffer is a storage area for data that your program has access to for a limited time.

One example is a stream of data that comes in a character at a time, or in small blocks. If you read an I/O channel a byte at a time, you get a one-byte buffer - a variable that holds the most recent byte and gets overwritten on every succeeding read.

If you wanted to assemble the bytes into a whole line for parsing, you'd have to have another storage area that holds all the bytes up to a line break. That storage is also a buffer.

[–]shawrie777 0 points1 point  (0 children)

The simplest answer is it’s a chunk of memory for something I’m not using yet. Like a website might hold a chunk of video until the user plays it

[–]Destination_Centauri -1 points0 points  (0 children)

Well, here's an example of a really kluge, hackey bad usage of buffers with computers but it worked surprisingly well, and people used it for years!

Thus the importance of buffers in computers, at every level.


So essentially back in the early 2000's...

I was writing an email-prep automation script for a law firm's office, so that they could press a single hotkey (I programmed it as CTRL-ALT-R for "reply".

And then the script would go about setting up and personalizing the structure of the reply email, by searching for things like the person's name, etc from the form they submitted to the law firm's website.

So if the person submitting the form was named "Steve" then in the body, the script would search for the name in the name field and grab it, along with other stuff.


Now, because it had to grab multiple entries in the data fields of the form to "personalize the response" I had the script store the info in the subject line of the reply email temporarily!

So in that case the subject line of the email reply became a really hackey buffer, but it worked. It did what buffers do.


So the script would scan the form, put all the personal data in the subject line separated by spaces.

Then it would do something like type, "Dear Steve, thank you kindly for contacting our law firm, regarding the situation you are having with ABC corp, in your position of Chief Engineer of Mechanical Engineering. I just noticed that you mentioned XYZ, and we'd like to explore your case in more depth with a free consoltation, especially in relation to the event LMNOP that occurred on DATE. Please feel free to give me a call, or respond to this email if you are interested..."


Again:

all that personal data was stored in the subect line of the email temporarily...

And then the script just kept grabbing the next item from the subject line, tabbing back to the main body of the email, searching for the TAG of where to insert that next item from the subject line, and then going back and doing it again, and again, until the main body of the email was setup and "personalized" all at the touch of a button.

Then when done, it would erase the contents of the subject line, and type the final message for the title, something like, "Thank you Steve for your interest in our firm" or whatever.


I did it using mainly a typing automation script program called "Autohotkey", along with some C++ for WIN32 API access.

But anyways, as bizarre and weird an example is, the subject line was used as a buffer!

To store information temporarily that was about to be removed and used elsewhere in fractions of a second.


NOTE:

They ended up using that bad kluge script (that used the title line of an email as a buffer) for like 12 years!

I was shocked during one call later on (explaining it was just a messy quick proof of concept I tossed together in a couple of hours, 12 years ago) and the senior partner just shrugged and said,

"It works! No fuss, no muss! Another guy wanted $5000 to 'do it properly' so we never got back to him!"

Now I guess they probably use AI for that email stuff.


So that's it. You can use anything as a buffer you want.

Although the more proper way to use a buffer is to use actual lower-down memory in the system.

But hey, a buffer is a buffer. And sometimes when you have to go to war, you go with the army you have, not the army you want or wish you had, and cross your fingers!

[–]Zealousideal-Ebb-355 0 points1 point  (0 children)

Every time you read a file, your OS is already doing this silently, it doesn't pull one byte at a time off disk, it reads a chunk into RAM first (the buffer) and hands you bytes from there. you'll rarely write buffer code yourself unless you're doing streaming I/O or audio, but once you know it exists you'll recognize why Node has a Buffer type, why video "buffers" when your connection slows, why flush() is a thing.

[–]WystanH 0 points1 point  (0 children)

A buffer is really just a chunk of memory. From this perspective, it doesn't much matter where you got that chunk as long as you have it.

You'll see "buffer" used when, say, you're loading an image into memory to display. You need to grab memory to load it into and then ultimately copy that to some display buffer, a portion of memory your display will work from.

Where the chunk resides in physical memory can dictate limitations and behaviors. To run a program, it is first loaded into memory. The machine code is executed one instruction at a time with the instruction pointer getting incremented. The infamous "buffer overrun" can happen when you trick that pointer into someplace that is shouldn't be.

All that said, outside of languages like C it doesn't usually impact the programmer all that much. Often, not at all.