What Does It Mean to Buffer in C++?

Buffering Speeds up the Calculation Process

Buffering symbols showing 75%, 50%, and 25%

lethutrang101 / Pixabay 

"Buffer" is a generic term that refers to a block of computer memory that serves as a temporary placeholder. You might encounter the term in your computer, which uses RAM as a buffer, or in video streaming where a section of the movie you are streaming downloads to your device to stay ahead of your viewing. Computer programmers use buffers as well.

Data Buffers in Programming

In computer programming, data can be placed in a software buffer before it is processed. Because writing data to a buffer is much faster than a direct operation, using a buffer while programming in C and C++ makes a lot of sense and speeds up the calculation process. Buffers come in handy when a difference exists between the rate data is received and the rate it is processed. 

Buffer vs. Cache

A buffer is temporary storage of data that is on its way to other media or storage of data that can be modified non-sequentially before it is read sequentially. It attempts to reduce the difference between input speed and output speed. A cache also acts as a buffer, but it stores data that is expected to be read several times to reduce the need to access slower storage. 

How to Create a Buffer in C++

Usually, when you open a file, a buffer is created. When you close the file, the buffer is flushed. When working in C++, you can create a buffer by allocating memory in this manner:

char* buffer = new char[length];

When you want to free up the memory allocated to a buffer, you do so like this:

delete[ ] buffer;

Note: If your system is low on memory, the benefits of buffering suffer. At this point, you have to find a balance between the size of a buffer and the available memory of your computer.

 

Format
mla apa chicago
Your Citation
Bolton, David. "What Does It Mean to Buffer in C++?" ThoughtCo, Aug. 28, 2020, thoughtco.com/definition-of-buffer-p2-958030. Bolton, David. (2020, August 28). What Does It Mean to Buffer in C++? Retrieved from https://www.thoughtco.com/definition-of-buffer-p2-958030 Bolton, David. "What Does It Mean to Buffer in C++?" ThoughtCo. https://www.thoughtco.com/definition-of-buffer-p2-958030 (accessed April 16, 2024).