A buffer is a temporary storage area – usually a block of RAM – that holds data while it is being transferred between a producer (e.g., a peripheral) and a consumer (e.g., the CPU). By decoupling the two sides, a buffer:
Buffers are not only a software concept. In a typical computer they sit on the address/data bus between a peripheral’s registers and main memory.
| Buffer type | Typical use (syllabus example) | Key characteristics |
|---|---|---|
| Input buffer | Keyboard, network card, file‑read operations | FIFO queue; protects against data loss when the CPU is busy. |
| Output buffer | Printer, display, file‑write operations | Allows the CPU to continue processing while the slower device finishes transmission. |
| Double (ping‑pong) buffer | Graphics rendering, audio streaming | Two buffers used alternately – one filled while the other emptied – giving a seamless flow. |
| Circular (ring) buffer | Real‑time sensor logging, continuous network capture | Fixed‑size ring; newest data overwrite oldest when full. |
| Hardware‑level buffer | Cache‑line buffers, DMA buffers, bus‑interface buffers inside a peripheral | Implemented in silicon; hides bus‑timing differences and can move data without CPU involvement. |
Consider a 4‑byte FIFO input buffer. The peripheral produces a byte every 2 ms, while the CPU reads one byte every 5 ms.
This simple timeline shows how overflow can arise when the producer’s rate exceeds the consumer’s average rate.
fread, fwrite) read/write blocks of data to minimise disk‑access latency.Cambridge‑style FIFO implementation (size = 8 bytes). The code follows the pseudocode conventions used in the syllabus (e.g., ENDIF, ENDWHILE).
CONST SIZE = 8ARRAY BYTE buffer[SIZE]
INTEGER head = 0 // index of next byte to read
INTEGER tail = 0 // index of next free slot
INTEGER count = 0 // number of bytes currently stored
PROCEDURE enqueue(BYTE data) RETURNS BOOLEAN
IF count = SIZE THEN // buffer full → overflow
RETURN FALSE // reject the byte
ENDIF
buffer[tail] := data
tail := (tail + 1) MOD SIZE
count := count + 1
RETURN TRUE
ENDPROCEDURE
PROCEDURE dequeue() RETURNS (BOOLEAN, BYTE)
IF count = 0 THEN // buffer empty → underflow
RETURN (FALSE, 0)
ENDIF
BYTE data := buffer[head]
head := (head + 1) MOD SIZE
count := count - 1
RETURN (TRUE, data)
ENDPROCEDURE
strncpy instead of strcpy).Q1 (extended response, 6 marks): A peripheral writes data to a 16‑byte input buffer. Explain what happens when the buffer is already full and the peripheral attempts to write another byte. In your answer, mention at least two possible consequences for the system.
Suggested points for full credit:
- The write is rejected; the peripheral must either block (wait) or discard the byte (non‑blocking I/O).
- If discarded, data loss occurs; if the system later reads beyond the buffer’s limits, a buffer overflow can corrupt memory, leading to crashes or security exploits.
- System‑level impact – reduced reliability, possible deadlock if the CPU waits for data that will never arrive, or a security breach if malicious data overwrite adjacent memory.
Q2 (short answer, 4 marks): List three different types of buffers covered in the syllabus and give one practical example for each.
Answer outline:
- Input buffer – keyboard keystrokes.
- Double (ping‑pong) buffer – graphics frame rendering.
- Circular buffer – real‑time audio capture.
Q3 (pseudocode, 6 marks): Write Cambridge‑style pseudocode for a routine that removes (dequeues) a byte from a circular buffer and returns a Boolean indicating success. Include comments that show how overflow/underflow is prevented.
Full‑credit answer (excerpt):
PROCEDURE dequeue() RETURNS (BOOLEAN, BYTE)IF count = 0 THEN // underflow – buffer empty
RETURN (FALSE, 0)
ENDIF
BYTE data := buffer[head] // read oldest byte
head := (head + 1) MOD SIZE // advance read pointer
count := count - 1 // one less byte stored
RETURN (TRUE, data) // successful read
ENDPROCEDURE
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.