Per - Per-class New/Delete Some types of applications tend...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Per-class New/Delete Some types of applications tend to use many small blocks of space for allocating nodes for particular types of data structures, small strings, and so on. In issue #002 we talked about a technique for efficiently allocating many small strings. Another way of tackling this problem is to overload the new/delete operators on a per- class basis. That is, take over responsibility for allocating and deallocating the storage required by class objects. Here is an example of what this would look like for a class A: #include <stddef.h> #include <stdlib.h> class A { int data; A* next; #ifdef USE_ND static A* freelist; // pointer to free list #endif public: A(); ~A(); #ifdef USE_ND void* operator new(size_t); // overloaded new() void operator delete(void*); // overloaded delete() #endif }; #ifdef USE_ND A* A::freelist = 0; inline void* A::operator new(size_t sz) { // get free node from freelist if any if (freelist) { A* p = freelist; freelist = freelist->next; return p; } // call malloc() otherwise
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 2

Per - Per-class New/Delete Some types of applications tend...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online