

buffer                      Definition                     buffer




A 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 malloc or its related functions.

Buffering,  and problems  therewith, are  encountered  most often
when using the  standard input and output (STDIO) routines.  Many
operating systems  (including COHERENT) automatically  place data
from a peripheral device  into a buffer.  Buffers normally can be
cleared  with 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 fclose, which closes
a file  stream, flushes all buffers  associated with that stream.
exit calls fclose.

Combining unbuffered and  buffered I/O functions on the same file
or device  within one  program will  produce results that  are at
best unpredictable.

***** Example *****

The following example  demonstrates what does and does not happen
when you use fffflluusshh with the output buffer.


#include <stdio.h>
main()
{
        extern char *malloc();
        char *buffer;



        /* use malloc() to create a 120-char buffer */
        if ((buffer = malloc(120)) == NULL) {
                /* if malloc() fails, bail out */
                fprintf(stderr, "malloc failed\n");
                exit(1);
        }



        printf("Type your name:  ");
        fflush(stdout);
        gets(buffer);
        printf("Your name is %s\n", buffer);
}


***** See Also *****

arena, array, close, definitions, exit, fflush, malloc, STDIO




COHERENT Lexicon                                           Page 1


