home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 3.ddi / STARTUP.ZIP / EXAMPLES / MALLOC / MALLOC.C < prev   
Encoding:
C/C++ Source or Header  |  1990-09-01  |  1001 b   |  48 lines

  1. /*
  2. //        Sample application to test dynamic memory allocation by randomly
  3. //        allocated and freeing random sized blocks of memory.  This demo
  4. //        requires the Paradigm MS-DOS emulator to be present.
  5. //
  6. //        Memory management takes place on the heap for small data memory
  7. //        models and on the far heap for large data models.
  8. */
  9.  
  10. #include    <stddef.h>
  11. #include    <stdlib.h>
  12. #include    <alloc.h>
  13. #include    <mem.h>
  14.  
  15. #define    ALLOCS        250
  16. #define    BLOCK_SIZE    100
  17.  
  18. char    *p[ALLOCS] ;
  19.  
  20.  
  21. void    main(void)
  22. {
  23.     unsigned    int    item ;
  24.     unsigned    int    size ;
  25.     unsigned    long    allocs ;
  26.     unsigned int    errors ;
  27.  
  28.     allocs = errors = 0 ;
  29.     while (1)   {
  30.         /* Compute the pointer and allocation size */
  31.         item = random(ALLOCS) ;
  32.         size = random(BLOCK_SIZE) + 1 ;
  33.  
  34.         /* Free up the memory if in use */
  35.         if (p[item] != NULL)
  36.             free(p[item]) ;
  37.  
  38.         /* Allocate a new block */
  39.         if ((p[item] = malloc(size)) == NULL)
  40.             errors++ ;
  41.         else   {
  42.             allocs++ ;
  43.             memset(p[item], item, size) ;
  44.         }
  45.     }
  46. }
  47.  
  48.