`

c++ - operator new and delete and an example of linked list stores by new/delete

    博客分类:
  • c++
c++ 
阅读更多

The operator new and delete operator can overloaded to achieve some customed memory management strategy, suppose we have a Sscreen object, and we want to allocate a chunk of memory instead of allocate each at a time when call the new operator to improve memory usage effeciency. Here is what we can do .

 

suppose we have the following members. 

 

/**
* file 
*   Screen_Alloc.h
*
*  description:
*   This is the code that shows you how to overload the allocation
*/

class Screen
{
public:
	void * operator new(size_t );

	void operator delete(void *, size_t);
private:
	Screen * next;
	static Screen *freeStore;
	static const int screenChunk;
protected:
};

 

We uses a static member to hold the free list pointer, and the screenChunk side control how much bunks we are going to allocate in one go.

 

and below is the implementation code. 

 

/**
* file 
*   Screen_Alloc.cpp
*
*  description:
*   This is the code that shows you how to overload the allocation
*/

#include "stdafx.h"
#include "Screen_Alloc.h"

// static member are initialized within program text files, no header files 
Screen * Screen::freeStore = 0;
const int Screen::screenChunk =  24;

void *Screen::operator new(size_t size)
{
	Screen *p;
	if (freeStore == 0) 
	{
		size_t chunk = screenChunk * size; // the size is populated by the compiler and is the size of Screen
		freeStore = p = reinterpret_cast<Screen *>(new char[chunk]);
		// now thread the memory allocated
		for (; p != &freeStore[screenChunk - 1]; ++p) {
			p->next = p + 1;
		}
		p-> next = 0;

		p = freeStore;
		freeStore = freeStore->next;
		return p;
	}
}

/** 
* the delete expression 
*    delete ptr 
* is equivalent to 
*    Screen::~Screen(ptr);
*    Screen::operator delete(ptr, sizeof(*ptr); 
*/
void Screen::operator delete (void *p, size_t)
{
	// inser the 'deleted' object back
	// into the free list
	(static_cast<Screen *> (p) )-> next = freeStore;
	freeStore = static_cast<Screen*>(p);
}

 

 

Array operator new[] and operator delete[]

 

when you override the default operator new and operator delete,you may/may not defined the array operator [] and the array delete operator []...

 

 

void * operator new[](size_t);
void operator delete[](void *, size_t);

 

to invoke the new[] operator you do this

 

Screen* screenptr = new Screen[10];

 

what it does, first allocate the size enough to hold the array, and for each of the element, it calles the default constructor to initialize the objct, and when this is all done, return the pointer to the caller. 

 

 

to call the delete[] operator you can do this:

 

delete[] screenptr;// screenptr point to the Screen array

 what it does is 

 

first call the destructors iteratively, and then deallocate the memory of the array;  

A common mistake is to call 

 

delete screenptr;
 

it may deallocate the correct number of memory , but only the first objec't destructor will likely be called. 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics