Abort,__FILE__ and __LINE__

It is quite common to ignore the error return codes of standard library functions. This is dangerous. In the case of the int_stack example, malloc could fail because it had run out of memory.

The handling of library and system errors depends on the nature of the error. In the case of running out of memory, usually the best we can do is print out an error message and exit the program.

	#include <stdlib.h>
	#include <stdio.h>

	  int_stack	stack = xalloc(struct int_stack);
	  if( stack == 0 ) {
	    fprintf(stderr, "%s %d: out of memory\n", __FILE__, __LINE__);
	  x_max_int_stack(stack) = 16;
	  x_len_int_stack(stack) = 0;
	  x_vals_int_stack(stack) = xnalloc(int, 16);
	  if( x_vals_int_stack(stack) == 0 ) {
	    fprintf(stderr, "%s %d: out of memory\n", __FILE__, __LINE__);

This code uses 3 C constructs to implement the error code:

Warning: __FILE__ and __LINE__ were both standardized by ANSI C; they may not be supported by (really) old compilers.

Next Prev Main Top Feedback