What does it mean by buffer?

后端 未结 8 1203
自闭症患者
自闭症患者 2020-12-04 05:04

I see the word \"BUFFER\" everywhere, but I am unable to grasp what it exactly is.

  1. Would anybody please explain what is buffer in laym
8条回答
  •  没有蜡笔的小新
    2020-12-04 05:11

    A buffer is simply a chunk of memory used to hold data. In the most general sense, it's usually a single blob of memory that's loaded in one operation, and then emptied in one or more, Perchik's "candy bowl" example. In a C program, for example, you might have:

    #define BUFSIZE 1024
    char buffer[BUFSIZE];
    size_t len = ;
    
    // ... later
    while((len=read(STDIN, &buffer, BUFSIZE)) > 0)
        write(STDOUT, buffer, len);
    

    ... which is a minimal version of cp(1). Here, the buffer array is used to store the data read by read(2) until it's written; then the buffer is re-used.

    There are more complicated buffer schemes used, for example a circular buffer, where some finite number of buffers are used, one after the next; once the buffers are all full, the index "wraps around" so that the first one is re-used.

提交回复
热议问题