.TH buffer "" "" Definition
.PC
.PP
A
.I buffer
is a portion of memory set aside
to hold data read from or to be written to another process or device.
Often, although not always, this
involves setting aside a portion of the arena with
.B malloc
or its related functions.
.PP
Buffering, and problems therewith, are encountered most often when
using the standard input and output (STDIO) routines.
Many operating systems
(including \*(CO)
automatically place data from a peripheral device into a buffer.
Buffers normally can be cleared with
.BR fflush() ,
by pressing the carriage return key on routines that perform
input, or by sending a newline character on routines that perform output.
The function
.BR fclose() ,
which closes a file stream, flushes all buffers associated with that stream.
.B exit()
calls
.BR fclose() .
.PP
Combining unbuffered and buffered I/O functions on
the same file or device within one program
will produce results that are at best unpredictable.
.SH Example
The following example demonstrates what does and does not happen
when you use \fBfflush()\fR with the output buffer.
.DM
#include <stdio.h>
main()
{
	extern char *malloc();
	char *buffer;
.DE
.DM
	/* use malloc() to create a 120-char buffer */
	if ((buffer = malloc(120)) == NULL) {
		/* if malloc() fails, bail out */
		fprintf(stderr, "malloc failed\en");
		exit(1);
	}
.DE
.DM
	printf("Type your name:  ");
	fflush(stdout);
	gets(buffer);
	printf("Your name is %s\en", buffer);
}
.DE
.SH "See Also"
.Xr arena, arena
.Xr array, array
.Xr close(), close
.Xr exit(), exit
.Xr fflush(), fflush
.Xr malloc(), malloc
.Xr "Programming COHERENT," programmi
.Xr stdio.h stdio.h
