[Previous]
[Contents]
[Next]

stackavail()

return the number of bytes currently available in the stack

Synopsis:

#include <alloca.h>
size_t stackavail(void);

Description:

The stackavail() function returns the number of bytes currently available in the stack. This value is usually used to determine an appropriate amount to allocate using alloca().

Returns:

The number of bytes currently available in the stack.

Examples:

#include <stdio.h>
#include <string.h>
#include <alloca.h>
#include <fcntl.h>
#include <unistd.h>

long char_count( FILE *fp )
  {
     char *buffer;
     size_t bufsiz;
     long count;

     /* allocate half of stack for temp buffer */
     bufsiz = stackavail() >> 1;
     buffer = (char *) alloca( bufsiz );
     setvbuf( fp, buffer, _IOFBF, bufsiz );
     count = 0L;
     while( fgetc( fp ) != EOF ) ++count;
     fclose( fp );
     return( count );
  }

void main()
  {
    FILE *fp;

    fp = fopen( "file", "rb" );
    if( fp != NULL ) {
      setmode( fileno( fp ), O_BINARY );
      printf( "File contains %lu characters\n",
      char_count( fp ) );
      fclose( fp );
    }
  }

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler Yes
Thread Yes

See also:

alloca(), calloc(), malloc()


[Previous]
[Contents]
[Next]