#include #include #include "cookie.h" using namespace std; char cookie::buffer[n * sizeof (cookie)]; bool cookie::b[n]; void *cookie::operator new(size_t) { for (size_t i = 0; i < n; ++i) { if (!b[i]) { b[i] = true; cout << "cookie::operator new allocates subscript " << i << ".\n"; return reinterpret_cast(buffer) + i; } } cerr << "cookie::operator new out of store\n"; exit(EXIT_FAILURE); } void cookie::operator delete(void *p) { const ptrdiff_t d = static_cast(p) - buffer; if (d < 0 || d % sizeof (cookie) != 0) { cerr << "cookie::operator delete argument " << p << " didn't come from cookie::operator new.\n"; exit(EXIT_FAILURE); } const size_t i = static_cast(p) - reinterpret_cast(buffer); if (i >= n) { cerr << "cookie::operator delete argument " << p << " didn't come from cookie::operator new.\n"; exit(EXIT_FAILURE); } cout << "cookie::operator delete deallocates subscript " << i << ".\n"; b[i] = false; }