home *** CD-ROM | disk | FTP | other *** search
/ C/C++ User's Journal & Wi…eveloper's Journal Tools / C-C__Users_Journal_and_Windows_Developers_Journal_Tools_1997.iso / windbase / windbase.exe / MEMSLC.3 / MEMORY.C < prev    next >
C/C++ Source or Header  |  1996-07-14  |  7KB  |  170 lines

  1. /*****************************************************************************\
  2. **                                                                           **
  3. **  WW     WW IIIIIIII NNN   NN DDDDDDD  BBBBBBB     AA     SSSSSS EEEEEEEE  **
  4. **  WW  W  WW    II    NNNN  NN DD    DD BB    BB  AA  AA  SS      EE        **
  5. **  WW  W  WW    II    NN NN NN DD    DD BBBBBBB  AAAAAAAA  SSSSSS EEEEEE    **
  6. **   WW W WW     II    NN  NNNN DD    DD BB    BB AA    AA      SS EE        **
  7. **    WWWWW   IIIIIIII NN   NNN DDDDDDD  BBBBBBB  AA    AA SSSSSS  EEEEEEEE  **
  8. **                                                                           **
  9. **   SSSSSS  OOOOOO  FFFFFFFF TTTTTTTT WW     WW    AA    RRRRRRR  EEEEEEEE  **
  10. **  SS      OO    OO FF          TT    WW  W  WW  AA  AA  RR    RR EE        **
  11. **   SSSSS  OO    OO FFFFF       TT    WW  W  WW AAAAAAAA RRRRRRR  EEEEEE    **
  12. **       SS OO    OO FF          TT     WW W WW  AA    AA RR   RR  EE        **
  13. **  SSSSSS   OOOOOO  FF          TT      WWWWW   AA    AA RR    RR EEEEEEEE  **
  14. **                                                                           **
  15. *********** NOTICE ************************************************************
  16. **        This file contains valuable trade secrets and proprietary          **
  17. **        assets of Windbase Software Inc.  Embodying substantial            **
  18. **        creative efforts and confidential information.  Unauthorized       **
  19. **        use, copying, decompiling, translating, disclosure or              **
  20. **        transfer, of any kind, is strictly prohibited.                     **
  21. **                                                                           **
  22. **        COPYRIGHT (C) 1992, 1993, 1994.  Windbase Software Inc.            **
  23. **        ALL RIGHTS RESERVED.                                               **
  24. \*****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #ifdef HAS_UNISTD_H
  28. #  include <unistd.h>
  29. #else
  30. #  include <stdlib.h>
  31. #endif
  32. #include <string.h>
  33. #include <malloc.h>
  34. #include <ctype.h>
  35.  
  36. #define WBMEMTRACE
  37. #include "../memsl.h"
  38.  
  39. #if defined(WBTRC_LEVEL1) || defined(WBTRC_LEVEL2)
  40.   #ifdef WBSTDC
  41.     int main(int argc, char **argv)
  42.   #else
  43.     int main(argc, argv)
  44.       int argc;
  45.       char **argv;
  46.   #endif
  47. #else
  48.   #ifdef WBSTDC
  49.     int main(void)
  50.   #else
  51.     int main()
  52.   #endif
  53. #endif
  54.   {
  55.     WBERROR *errhdl;
  56.     FILE *file;
  57.     char str[21], *strptr[201];
  58.     int i = 0, i2, ch;
  59.  
  60.     WBTrcMainEntry();
  61.  
  62.     memset(strptr,0,sizeof(strptr));
  63.     if ((errhdl = WBErrorOpen(0,"error.txt")) != NULL)
  64.       {
  65.         WBMallocErrH(errhdl);
  66.         if ((char *)getenv("WBMEMCHK") == NULL)
  67.           {
  68.         printf("\n\n************* WARNING *******************************\n\n");
  69.             printf("This program will crash, and likely crash your system because the two\n");
  70.             printf("environment variables are not defined.  The environment variables are:\n\n");
  71.  
  72.             printf("      WBMEMCHK - Turns memory checking on\n");
  73.             printf("      WBOVRCHK - Turns overwrite checking on\n\n");
  74.  
  75.             printf("With the two environment variables defined, the 'memory.exe' program\n");
  76.             printf("will not crash, but will instead display information regarding the\n");
  77.             printf("improper use of memory.  The above environment variables do not need\n");
  78.             printf("to be set to any particular value they just need to be set.  The\n");
  79.             printf("following example will suffice for setting the environment variables:\n\n");
  80.  
  81.             printf("      set WBMEMCHK=\"\"\n");
  82.         printf("      set WBOVRCHK=\"\"\n");
  83.         printf("    or\n");
  84.             printf("      WBMEMCHK=\"\"; export WBMEMCHK (for UNIX)\n");
  85.             printf("      WBOVRCHK=\"\"; export WBOVRCHK\n\n");
  86.  
  87.             printf("Be sure to unset the 'WBOVRCHK' and 'WBMEMCHK' environment variables\n");
  88.         printf("when you are done with the memory demo.  Leaving the 'WBOVRCHK' and\n");
  89.             printf("'WBMEMCHK' environment variable set will cause other MemSL demo\n");
  90.             printf("programs to run slowly.\n\n");
  91.  
  92.             printf("Would you like to continue WITHOUT the environment\n");
  93.             printf("variables set (y/n)? ");
  94.  
  95.             do { ch = getchar(); } while (strchr("yYnN",ch) == NULL);
  96.             if (strchr("nN",toupper(ch)))
  97.               {
  98.                 printf("\n\nAborting...\n");
  99.                 exit(0);
  100.               }
  101.     
  102.         printf("Are you sure you want to continue without\n");
  103.         printf("memory checking (y/n)? ");
  104.             do { ch = getchar(); } while (strchr("yYnN",ch) == NULL);
  105.             if (strchr("nN",toupper(ch)))
  106.               {
  107.                 printf("\n\nAborting...\n");
  108.         exit(0);
  109.               }
  110.           }
  111.  
  112.         if ((file = fopen("data.dat","r")) != NULL)
  113.           {
  114.             while (i < 200 && fgets(str,20,file))
  115.               {
  116.                 if (str[strlen(str)-1] == '\n')
  117.                   str[strlen(str)-1] = 0;
  118.                 if ((strptr[i] = malloc(strlen(str)+1)) != NULL)
  119.                   strcpy(strptr[i],str);
  120.                 i++;
  121.               }
  122.         fclose(file);
  123.  
  124.             /* Starting at 1 should generate a still allocated error. */
  125.             /* Extra free should generate an error. */
  126.             for (i2 = 1; i2 < i+1; i2++)
  127.               free(strptr[i2]);
  128.  
  129.             /*
  130.             ** Common error of not including an extra byte for the
  131.             ** NULL terminator ending a string.
  132.             */
  133.             strptr[0] = calloc(1,9);
  134.             strcpy(strptr[0],"Overwrite");
  135.             free(strptr[0]);
  136.     
  137.             /* Overwrite at beggining */
  138.             strptr[0] = malloc(9);
  139.             strcpy(strptr[0]-1,"Overwrite");
  140.             free(strptr[0]);
  141.     
  142.         /* Zero allocation */
  143.         strptr[0] = malloc(0);
  144.     
  145.             /* NULL Free */
  146.             free(NULL);
  147.     
  148.         /* Normal call to realloc */
  149.             strptr[0] = realloc(NULL,10);
  150.     
  151.             /*
  152.             ** This call to realloc should generate
  153.             ** a still allocated message.
  154.             */
  155.             strptr[0] = realloc(strptr[0],20);
  156.     
  157.             /*
  158.             ** This realloc should generate both an
  159.             ** invalid free and a still allocated message.
  160.             */
  161.             strptr[0] = realloc(strptr[1],30);
  162.           }
  163.       }
  164.    
  165.     if (getenv("WBMEMCHK"))
  166.       printf("\nThe error output can be found in 'error.txt'\n\n");
  167.  
  168.     WBTrcReturn(0,0,("0"));
  169.   }
  170.