# include # include "xmalloc.h" //**************************************************************************************80 int main ( ) //**************************************************************************************80 // // Purpose: // // xmalloc_test1 demonstrates simple calls to xmalloc. // // License: // // I don't care what the hell you do with this code. // // Modified: // // 07 July 2017 // // Author: // // John Burkardt // { int n = 1000; size_t nbytes; int *x; printf ( "\n" ); printf ( "xmalloc_test1:\n" ); printf ( " Test xmalloc for memory allocation.\n" ); nbytes = n * sizeof ( int ); printf ( "\n" ); printf ( " Request %zu bytes of memory using malloc:\n", nbytes ); x = ( int * ) malloc ( nbytes ); free ( x ); printf ( "\n" ); printf ( " Request %zu bytes of memory using malloc_or_exit:\n", nbytes ); x = ( int * ) malloc_or_exit ( nbytes, __FILE__, __LINE__ ); free ( x ); printf ( "\n" ); printf ( " Request %zu bytes of memory using ymalloc:\n", nbytes ); x = ( int * ) ymalloc ( nbytes ); free ( x ); // // The author made a revolting choice in defining xmalloc; // I have registered my opinion of this code by renaming it. // I wasted the requisite half hour wondering why calls failed that // included spaces, or a different name for the size variable. // printf ( "\n" ); printf ( " Request %zu bytes of memory using xmalloc:\n", nbytes ); printf ( " Must be used LITERALLY as 'xmalloc(nbytes)'\n" ); printf ( " no spaces, no changes whatsoever!\n" ); x = xmalloc(nbytes); free ( x ); // // Demonstrate that xmalloc will produce an error message for a request of 0 bytes. // nbytes = 0; printf ( "\n" ); printf ( " Request %zu bytes of memory using xmalloc(nbytes):\n", nbytes ); x = xmalloc(nbytes); free ( x ); // // Terminate. // printf ( "\n" ); printf ( "xmalloc_test1:\n" ); printf ( " Normal end of execution.\n" ); return 0; }