home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pctchnqs / 1991 / number6 / newdel.cpp < prev    next >
Text File  |  1991-11-01  |  828b  |  41 lines

  1. #include <alloc.h>
  2. #include <iostream.h>
  3.  
  4. #define TABLE_SIZE  100
  5.  
  6. static void *validaddr[TABLE_SIZE];
  7.  
  8. ///
  9. // Call C malloc() function and save pointer in internal table.
  10. void *operator new(size_t s)
  11. {
  12. void *p = malloc(s);
  13.  
  14. // Search for free space in table.
  15. for (int i = 0; i < TABLE_SIZE && validaddr[i] != 0; i++);
  16.  
  17. // Insert pointer into table if space found.
  18. if (i != TABLE_SIZE)
  19.   validaddr[i] = p;
  20.  
  21. return (p);
  22. }
  23.  
  24. ///
  25. // Test pointer for validity before deleting it.
  26. void operator delete(void *p)
  27. {
  28. if (p)
  29.   {
  30.   for (int i = 0; i < TABLE_SIZE && p != validaddr[i]; i++);
  31.   if (i != TABLE_SIZE)
  32.     {
  33.     validaddr[i] = 0;
  34.     free(p);
  35.     }
  36.   else
  37.     // *** SET BREAKPOINT HERE ***
  38.     cerr << "MEM MGR ERROR: delete called with invalid pointer "
  39.     << p << '.' << endl;   }
  40. }
  41.