Re: Corresponding forms of new and delete

From: Thomas Richter (
Date: 10/21/03

Date: 21 Oct 2003 15:56:34 -0400

In comp.lang.c++.moderated Dave <> wrote:

> Hello all,

> In the code below, I see the following output:

> base::operator new(size_t, int)
> base::base()
> base::~base()
> base::operator delete(void *)

> When the statement "delete ptr;" is reached, is there a way to cause it to
> use "operator delete(void*, int)" as the underlying deallocation routine
> rather than "operator delete(void *)"?

Short and simple: No.

If you require additional environment information for the memory allocation
or deallocation, you have to store it along with the memory allocated.

A typical solution would be to allocate "one additional int" along with the
requested memory and store the "int argument" of new in there. Then, in
the delete operator, you need to extract it from there.

Basically (unchecked source, so keep care!)

void *base::operator new(size_t s, int x)
 size_t *mem = (size_t *)malloc(s + sizeof(int));
 *mem = x;
 return mem+1;

void base::operator delete(void *mem)
 size_t *mymem = (size_t *)mem;
 int x = mymem[-1]; // extract private information.

The above code still requires work for architectures where the alignment
restrictions for size_t are insufficient for some other objects, but it
should give you the general idea how to solve this problem.


      [ See for info about ]
      [ comp.lang.c++.moderated. First time posters: Do this! ]