简体   繁体   English

我对缓冲区的理解正确吗?

[英]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++. 我认为这没有什么区别,但是我使用的语言是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. 这通常隐藏在C或C ++运行时中。

In C++, the internal implementation of std::stream uses std::streambuf to handle a low-level buffering mechanism for file-I/O. 在C ++中, std::stream的内部实现使用std::streambuf来处理文件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. 另一个示例是使用OpenCL(或例如Cuda)处理GPU上的某些数据并想要一些内存来存储数据的应用程序,然后调用clCreateBuffer(..., size, ...)将返回一个用于存储size字节的内存对象。

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]). (以及在计算机外部的区域,例如化学方面的化合物(一种能够抵抗或帮助限制pH值的化合物)和火车上的区域(在支架末端的“碰撞停止”))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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