home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / NEWDEL2.CP$ / NEWDEL2
Encoding:
Text File  |  1991-12-12  |  803 b   |  36 lines

  1. // NEWDEL2.CPP
  2.  
  3. // This is an example program from Chapter 6 of the C++ Tutorial. This
  4. //     program demonstrates customized new and delete operators with
  5. //     character fill.
  6.  
  7. #include <iostream.h>
  8. #include <malloc.h>
  9. #include <string.h>
  10.  
  11. // ------------- Overloaded new operator
  12. void *operator new( size_t size, int filler )
  13. {
  14.     void *rtn;
  15.     if( (rtn = malloc( size )) != NULL )
  16.         memset( rtn, filler, size );
  17.     return rtn;
  18. }
  19.  
  20. // ----------- Overloaded delete operator
  21. void operator delete( void *ptr )
  22. {
  23.     free( ptr );
  24. }
  25.  
  26. void main()
  27. {
  28.     // Allocate an asterisk-filled array
  29.     char *cp = new( '*' ) char[10];
  30.     // Display the array
  31.     for( int i = 0; i < 10; i++ )
  32.         cout << " " << cp[i];
  33.     // Release the memory
  34.     delete [] cp;
  35. }
  36.