home *** CD-ROM | disk | FTP | other *** search
- // ex03009.cpp
- // Home-brew new and delete with delete contention
- #include <iostream.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stddef.h>
-
- // ------------- overloaded new operator
- static void *operator new(size_t size, int filler)
- {
- cout << "\nRunning new\n";
- void *rtn = malloc(size);
- if (rtn != NULL)
- memset(rtn, filler, size);
- return rtn;
- }
-
- // ----------- overloaded delete operator
- void operator delete(void *type)
- {
- cout << "\nDeleting";
- free(type);
- }
-
- main()
- {
- // ------ allocate an array with the custom new
- char *cp = new ('*') char[10];
- // ---- use the default new for this allocation
- int *ip = new int[10];
- // ----- release the memory (both use our delete)
- delete ip;
- delete cp;
- }