Computer buffer

Asked

Viewed 403 times

6

I’ve read several places about buffers and there they say that it is a place in memory to store temporary values so they give an example like in C:

char exemplo[10];

and say that this is a buffer, but the vector exemplo this within the function int main then it would not be allocated in the stack?

2 answers

6

More than "store temporary values", the function of a buffer is to group into data sets that would be too small to be treated individually. When a process (usually a communication) has a overhead fixed for each data processed, the higher this data the less significance of the overhead as to what the process makes useful.

The best examples I can give refer to communication, but as we are talking about C I will give an example of reading a file on the hard drive. Let’s say for some reason you want to read a character-by-character file and do some relatively time-consuming operation with each of them. What happens when you ask the computer to read a character from the file?

  1. The hard drive, if stopped, starts running. Most of the time, it will already be running;

  2. Depending on where the data is in relation to the read head, the disk can rotate from nothing to an almost complete revolution until it reaches the character you want. Once he gets there, he copies the character to memory;

  3. Once in memory, your program can access it. It does something with it, and asks for the second character of the file;

  4. It turns out that the disk didn’t "brake" on that specific character you read: it kept running because it would be impractical for it to stop at that exact point of the last read (first because it is perhaps physically impossible, given its speed, second because even if it is possible it would waste a lot of energy and/or wear the materials of the disc, and third because it may have other programs running on the computer that may be waiting to also access the data on the disk);

  5. The consequence is that the character you want to read now, one after what you just read, has already "stayed behind": the disc would have to go a complete turn until the reading head returned to the point you want to read, and your program would run as slowly as it takes for your hard disk to run (and not at a more proportional speed to clock of your processor).

What is the solution? Instead of reading a single character of the file, you read several at once, storing them in a temporary area in memory to read hers next time you want another character. Thus, while the hard drive is running it already copies several characters at once into memory - in a single fraction of a rotation - and its program consumes this memory data with the latency relative to that media (and its caches), and not with the latency relative to the hard disk.

The fact that you’re in stack or in the heap is irrelevant [for that purpose], as explained by Maniero. What matters is that you took data from somewhere - the disk, an external storage, a socket... - and "queued" these data in memory, to consume them at the most appropriate speed for their particular use, regardless of the best treatment of them in their origin. Similarly, you can use a buffer to go storing the data you produce, and only send them to their final destination when they have a sufficient volume to be well treated by their destination.

2

Buffer (the article is not the most elucidative) it is essentially an abstract concept. There is no area of memory that is a buffer because any area can be one. You define what is one buffer. He’s just a set of bytes which is normally stored in a variable temporarily.

In C it is usually organized through a array allocated somewhere in memory. In this cited example it is allocated in the stack, but nothing prevents it from being allocated in the heap also.

Nothing stops you from calling your own buffer of exemplo but the most common is to give a name that makes it clear that it will be used as buffer, so Vari’s dream could be buffer. It doesn’t change anything technically but makes the intention clearer, and this is important when we’re coding.

And of course a variable called buffer can be used for other things as well, it is only recommended not to do so because of an organization.

A buffer is used to store any information that comes from somewhere. The details of how this will be done do not concern the buffer. It just needs to be allocated somewhere, and the program will define where, and have enough space for what is desired at that time.

So, technically the buffer there is, it’s just a form of nomenclature for something common to help better understand what you’re doing.

This is valid at least within the context of software programming, especially in C and other high-level languages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.