home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_07 / v7n7112a.txt < prev    next >
Text File  |  1989-09-05  |  3KB  |  165 lines

  1. /*
  2.  *    TITLE:    TEST.C
  3.  *
  4.  *    Test for HEAP program.
  5.  *
  6.  *    It turns out that the program needed some massaging.  It did
  7.  *    not display behavior I wanted it to, at its first try.
  8.  *
  9.  *    I left the trail code of my experiments in.
  10.  *
  11.  *    All programs were developed on an IBM PC/XT having an
  12.  *    Intel 386 inboard.
  13.  *    Compiler:      MS C 5.1
  14.  *    OS:            PC DOS 3.3
  15.  *
  16.  *    Avi Farah.
  17.  */
  18.  
  19.  
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <malloc.h>
  24. #include "heap.h"
  25.  
  26.  
  27.  
  28.  
  29. /*
  30.  *    #defines
  31.  */
  32. #define  LEN   20
  33.  
  34.  
  35.  
  36.  
  37. /*
  38.  *    Prototypes
  39.  */
  40. int   main( void );
  41. int   dummy1( void );
  42. int   dummy2( void );
  43.  
  44.  
  45.  
  46.  
  47. /*************************   main   ***************************************/
  48.  
  49. int  main()
  50.    {
  51.    char  *p, *q;
  52.    char far *fp;
  53.    int   err;
  54.  
  55.    MEMTRACE();          // Will report on the amount of local heap and stack
  56.    HEAPLOOK('\xCC');    // Initial look at the local/global heaps.
  57.  
  58.    /*
  59.     *    Dummy mallocs
  60.     */
  61.    if ((p = malloc(LEN)) == NULL)
  62.       {
  63.       printf("p = NULL cannot allocate local memory\n");
  64.       return 1;
  65.       }
  66.  
  67.    if ((q = malloc(LEN)) == NULL)
  68.       {
  69.       printf("q = NULL cannot allocate local memory\n");
  70.       return 2;
  71.       }
  72.  
  73. /*
  74.  *    Massaged code
  75.  */
  76. #  if 0
  77.    err = dummy1();
  78.    if (err)
  79.       return err;
  80. #  endif
  81.  
  82.    MEMTRACE();
  83.    HEAPLOOK('\xCC');    // mid point
  84.  
  85.    /*
  86.     *    Dummy malloc
  87.     */
  88.    if ((fp = _fmalloc(LEN)) == NULL)
  89.       {
  90.       printf("fp == NULL cannot allocate global memory\n");
  91.       return 3;
  92.       }
  93.  
  94.    err = dummy2();
  95.    if (err)
  96.       return err;
  97.  
  98.    MEMTRACE();
  99.    HEAPLOOK('\xCC');    // Final HEAPLOOK()
  100.  
  101.    _ffree(fp);
  102.    free(q);
  103.    free(p);
  104.  
  105.    return NULL;
  106.    }   /* main */
  107.  
  108.  
  109.  
  110.  
  111. /******************************   dummy1   **********************************/
  112.  
  113. int  dummy1()
  114.    {
  115.    char  *p;
  116.    char far *fp = 0;
  117.    register unsigned lc;
  118.  
  119.    if ((p = malloc(LEN)) == NULL)
  120.       {
  121.       printf("p = NULL cannot allocate local memory\n");
  122.       return 4;
  123.       }
  124.  
  125.    /*
  126.     *    Overflow !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  127.     */
  128.    for (lc = 0; lc < LEN; ++lc)
  129.       p[lc] = lc;
  130.    p[lc] = '\0';        // One byte overflow
  131.  
  132.    free(p);
  133.  
  134.    return NULL;
  135.    }   /* dummy1 */
  136.  
  137.  
  138.  
  139.  
  140. /******************************   dummy2   **********************************/
  141.  
  142. int  dummy2()
  143.    {
  144.    char  *p = 0;
  145.    char far *fp;
  146.    register unsigned lc;
  147.  
  148.    if ((fp = _fmalloc(LEN)) == NULL)
  149.       {
  150.       printf("fp == NULL cannot allocate global memory\n");
  151.       return 3;
  152.       }
  153.  
  154.    /*
  155.     *    Overflow !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  156.     */
  157.    for (lc = 0; lc < LEN + 3; ++lc)    // Overflow
  158.       fp[lc] = lc;
  159.  
  160.    _ffree(fp);
  161.  
  162.    return NULL;
  163.    }   /* dummy2 */
  164.  
  165.