# include # include "xmalloc.h" //**************************************************************************************80 void *ymalloc ( size_t nbytes ) //**************************************************************************************80 // // Purpose: // // ymalloc deals with a user request for memory. // // License: // // I don't care what you do with this code. // // Modified: // // 07 July 2017 // // Reference: // // Rouben Rostamian, // Programming Projects in C // for Students of Engineering, Science, and Mathematics, // SIAM, 2014, // ISBN: 978-1-611973-49-5 // // Parameters: // // Input, size_t nbytes, ... // // Output, void *ymalloc, ... // { void *x; x = malloc_or_exit ( nbytes, __FILE__, __LINE__ ); return x; } //**************************************************************************************80 void *malloc_or_exit ( size_t nbytes, const char *file, int line ) //**************************************************************************************80 // // Purpose: // // malloc_or_exit deals with a user request for memory. // // Discussion: // // Rostamian recommends an error return for a request of 0 bytes. // I don't see why. // // License: // // I don't care what you do with this code. // // Modified: // // 07 July 2017 // // Reference: // // Rouben Rostamian, // Programming Projects in C // for Students of Engineering, Science, and Mathematics, // SIAM, 2014, // ISBN: 978-1-611973-49-5 // // Parameters: // // Input, size_t nbytes, ... // // Input, const char *file, ... // // Input, int line, ... // // Output, void *malloc_or_exit, ... // { void *x; if ( nbytes == 0 ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "%s: line %d: Illegal request for malloc() of %zu bytes.\n", file, line, nbytes ); return NULL; } x = malloc ( nbytes ); if ( x == NULL ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "%s: line %d: malloc() of %zu bytes failed.\n", file, line, nbytes ); exit ( EXIT_FAILURE ); } return x; }