return the number of bytes currently available in the stack
#include <alloca.h> size_t stackavail(void);
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().
The number of bytes currently available in the stack.
#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 ); } }
WATCOM
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |