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! ]

Relevant Pages

  • Re: Exchange store memory allocation error
    ... MSFT changed the method of memory allocation for the store which ... causes the error reporting to false activate. ... increase the error threshold ...
  • Segmentation fault
    ... about 90% through the array before it fails. ... int main ... int i, args, success; ... fprintf(stderr, "Memory allocation failed\n"); ...
  • Re: Designing a Finite State Machine DFA Recognizer for UTF-8
    ... const int &utf8_size, ... const BYTE *utf8, const int &utf8_size, ... If you give up doing memory allocation yourself, then you also give up the possibility of user generated memory leaks. ...
  • p = new double [10], d = (double *) malloc (10 * sizeof (double));
    ... Note that indeed the character 'AB' is of type int and most significant byte is saved in ASCII for character 'B', and the least significant ASCII for character 'A'. ... If a date is redefining global in a function, then the reference to global time can be made by the operator for resolution. ... However in C language can be done to amend the current parameters, using pointers. ... Dezalocare for memory allocation and can be used in C language functions malloc and free. ...
  • Re: Simple DirectX development platform?
    ... and then optimizes for the architecture of that machine. ... Memory allocation and deallocation tends to be slow, ... object-oriented programming tends to do a lot of it. ... and deallocation of objects. ...