8

I know you can overload the operator new. When you do, you method gets sent a size_t parameter by default. However, is it possible to send the size_t parameter - as well as additional user-provided parameters, to the overloaded new operator method?

For example

int a = 5;
Monkey* monk = new Monkey(a);

Because I want to override new operator like this

void* Monkey::operator new(size_t size, int a)
{

...

}

Thanks

EDIT: Here's what I a want to accomplish:

I have a chunk of virtual memory allocated at the start of the app (a memory pool). All objects that inherit my base class will inherit its overloaded new operator. The reason I want to sometimes pass an argument in overloaded new is to tell my memory manager if I want to use the memory pool, or if I want to allocate it with malloc.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199
  • 1
    While this is possible, I would like to know what the motivating use case is. What do you want to pass as the `int` argument to `new`? – David Rodríguez - dribeas Dec 31 '11 at 00:57
  • 2
    Why do you want to overload `new`? What are you trying to accomplish exactly? – Remy Lebeau Dec 31 '11 at 00:59
  • That is interesting, how will you deallocate memory for your object? – marcinj Dec 31 '11 at 01:57
  • @luskan: you can likewise pass an extra argument to `delete` and you would e.g. pass the same memory pool used for `new` – Basile Starynkevitch Dec 31 '11 at 02:16
  • 1
    @BasileStarynkevitch: No, you can't pass an extra argument to `delete`. Placement versions of `operator delete` are only called when the constructor throws an exception. – Ben Voigt Dec 31 '11 at 03:02
  • @BenVoigt: Are you sure of that? Boehm's GC have a `void operator delete( void*, GCPlacement)` in its `class gc` inside ``? And I also have defined my delete operator with extra arguments. – Basile Starynkevitch Dec 31 '11 at 03:13
  • @BasileStarynkevitch: And those are called when construction fails. "If a *new-expression* calls a deallocation function, it passes the value returned from the allocation function call as the first argument of type `void*`. If a placement deallocation function is called, it is passed the same additional arguments as were passed to the placement allocation function, that is, the same arguments as those specified with the *new-placement* syntax." (section `[expr.new]`). But there is no placement syntax for the `delete` or `delete[]` operators. – Ben Voigt Dec 31 '11 at 03:15
  • http://www2.research.att.com/~bs/bs_faq2.html#placement-delete – Ben Voigt Dec 31 '11 at 03:18
  • I was not talking of the placement new provided by standard `` header; I was talking of providing my own `new` and `delete` operators... – Basile Starynkevitch Dec 31 '11 at 03:18
  • @BasileStarynkevitch: So is that section. – Ben Voigt Dec 31 '11 at 03:32

1 Answers1

14

Invoke new with that additional operand, e.g.

 Monkey *amonkey = new (1275) Monkey(a);

addenda

A practical example of passing argument[s] to your new operator is given by Boehm's garbage collector, which enables you to code

 Monkey *acollectedmonkey = new(UseGc) Monkey(a);

and then you don't have to bother about delete-ing acollectedmonkey (assuming its destructor don't do weird things; see this answer). These are the rare situations where you want to pass an explicit Allocator argument to template collections like std::vector or std::map.

When using memory pools, you often want to have some MemoryPool class, and pass instances (or pointers to them) of that class to your new and your delete operations. For readability reasons, I won't recommend referencing memory pools by some obscure integer number.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547