views:

142

answers:

7
+7  Q: 

What's a buffer?

Hello! As far as my understanding of languages goes, a buffer is any portion of memory in which a data is stored like an int,float variables, character arrays etc. However, I was reading buffer overflows and came across this link while reading about stack http://www.tenouk.com/Bufferoverflowc/Bufferoverflow2a.html The diagram in this link separates buffer from local variables of a function. Is this correct? What's a buffer then?

Thanks

A: 

Don't take that diagram too literally. Your first definition was correct. That buffer can be a local variable or it can be on the heap or in some other memory area... it's a very general concept.

Carl Smotricz
A: 

According to Wikipedia:

In computing, a buffer is a region of memory used to temporarily hold data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a keyboard) or just before it is sent to an output device (such as a printer). However, a buffer may be used when moving data between processes within a computer. This is comparable to buffers in telecommunication. Buffers can be implemented in either hardware or software, but the vast majority of buffers are implemented in software. Buffers are typically used when there is a difference between the rate at which data is received and the rate at which it can be processed, or in the case that these rates are variable, for example in a printer spooler or in online video streaming.

Usually a buffer in this sense would be an array containing many bytes of data, not a variable like an integer that can only hold a single value.

Mark Byers
+1  A: 

On the page that you linked to, think of "Buffer" as "the only local variable that we care about", and think of "Locally declared variables" as "All locally declared variables that aren't Buffer."

Dan Breslau
A: 

A buffer is just a block of memory used to store arbitrary data. In the diagram, I think that 'buffer' in intended to show a buffer which is declared as a local variable, for example char myString[80];. The danger is that if the length of the data being put into this buffer is not strictly monitored, you could e.g. strcpy(...) some data into the buffer and overflow the end - at which point the saved registers would be stomped over and a return from the function could (and almost certainly would) wreak havoc.

Will A
+1  A: 

As far as my understanding of languages goes, a buffer is any portion of memory in which a data is stored like an int,float variables, character arrays etc.

Well, not just any data, if that was the case all variables would be stored in buffers, and the term would be pointless.

A buffer is something that you use for temporary storage when data comes from one place and goes to another. Usually a buffer holds a lot more than a single variable, but there is of course special cases where a buffer is quite small also.

A local variable could be used as a buffer, so that would be allocated on the stack, but as buffers are usually large it would be impractical to use up a lot of stack space, so they are usually allocated somewhere else.

One example where a buffer is used, is when your program read from a file. At the lower level a disk can only be read in units of sectors, so the system reads a bunch of sectors into a buffer, and then your program reads from the buffer.

Guffa
A: 

Very abstract analogy: Local variables are what you're working with right now; they're what you're holding in your hands. A buffer is to a data source as a spoon is to a soup bowl, or a measuring cup is to a water tap. It's more practical to be holding a spoon in your hands than it is to be holding a soup bowl in your hands, and it's virtually impossible to be holding a running stream of water in your hands. In either case, you're using these utensils so that you can consume the soup/water in a pace that suits you.

More concretely, local variables are simply the variables that you've declared inside a function as opposed to outside. A buffer is a chunk of memory (usually an array) that's used to copy a small chunk of data from a huge data source, so you can process it at whatever pace your computer or program can handle. You might declare the buffer outside of your function if you want another function to fill it up, or you might declare it as a local variable if you're going to be filling it up and using it yourself. It's a really general term.

Some examples:

  • An audio buffer might hold about 0.5 seconds worth of audio to copy from the sound card to the program's memory for the program to process, or from the program's memory to the sound card for output to the speakers. Your program could immediately decode an mp3 file and dump all the data at your card, but it would end up being on the order of a few hundred MB if it did that, and you wouldn't enjoy listening to music at 50x speed -- so it decodes just little by little, and stores it in a buffer.
  • A video buffer might be filled up in your memory and/or hard disk from YouTube when you start a video, so that you're not pausing every few seconds because the internet connection's too slow.
  • A program might use a character buffer to share some text from one function to another. If you use cin or ReadLine or gets or something to get some text input from the keyboard, it would be fair to call the string that it gets stored in a "character buffer". In this case, you'd declare the buffer as a local variable.
Rei Miyasaka
A: 

Thanks guys. It was very helpful.

Naruto
As this isn't an actual answer to your question, you should put this as either a comment on your question, comment on an answer, or edit your question.
Blair McMillan