简体   繁体   中英

Is my understanding of a buffer correct?

I've been learning about buffers recently, and I wanted to check if I understand them correctly. I have to read in a file in binary format, which results in a sequence of bytes. In order to use these bytes in my program, I have to store them in a vector, and then I can go through each element in the vector.

Thus, a buffer is not an actual data type like a vector, but rather something that temporarily stores data in a better/more accessible format so that it can be used. Is this definition correct? I don't think it makes any difference, but the language I'm using is C++.

I think you have it right for this particular case, you are using a temporary storage to read bytes into memory, and then reshape that into the actual data-format that your program needs.

However: the term buffer is used for MANY things, in different contexts.

For example a buffer can be the memory used to read from a file into memory, so that when the source requests a single character (or small number of bytes), the runtime library doesn't have to go all the way down into the OS kernel to ask for one or a small number of bytes, but amortizes that overhead for a kilobyte or a few kilobytes of data. This is often hidden inside the C or C++ runtime.

In C++, the internal implementation of std::stream uses std::streambuf to handle a low-level buffering mechanism for file-I/O.

In other cases, it's where your key-presses are stored when the system is busy doing something else, until the application has time to read from the keyboard input.

Likewise, there are buffers to read video-streams from the internet, before showing the actual video content on the screen. Because if the video player requested just a few bytes at a time, as they were used, the overhead of the request would make the playback very jittery.

Another example would be an application using OpenCL (or for example Cuda) to process some data on the GPU, and want some memory to store data, then a call to clCreateBuffer(..., size, ...) will give back a memory object to store size bytes.

There are many other places where the term buffer is used in computers. (And in areas well outside of computers, for example in chemistry [a compound that is resistant to or helps restrict changes in pH] and for trains [the "bump stops" at the end of a carriage]).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM