home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX03009.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  752 b   |  35 lines

  1. // ex03009.cpp
  2. // Home-brew new and delete with delete contention
  3. #include <iostream.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stddef.h>
  7.  
  8. // ------------- overloaded new operator
  9. static void *operator new(size_t size, int filler)
  10. {
  11.     cout << "\nRunning new\n";
  12.     void *rtn = malloc(size);
  13.     if (rtn != NULL)
  14.         memset(rtn, filler, size);
  15.     return rtn;
  16. }
  17.  
  18. // ----------- overloaded delete operator
  19. void operator delete(void *type)
  20. {
  21.     cout << "\nDeleting";
  22.     free(type);
  23. }
  24.  
  25. main()
  26. {
  27.     // ------ allocate an array with the custom new
  28.     char *cp = new ('*') char[10];
  29.     // ---- use the default new for this allocation
  30.     int *ip = new int[10];
  31.     // ----- release the memory (both use our delete)
  32.     delete ip;
  33.     delete cp;
  34. }
  35.