2

Is it possible to prevent new from being used at certain points in the code?

The legacy code that I am developing with has a requirement that there is no dynamic memory allocation after the bootstrap has completed. We now want to test this.

If I was starting the development from scratch then I could write my own wrapper and use that, or overload operator new in a common base class.

Is there a way of overloading global new and then calling it?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
DanS
  • 1,677
  • 20
  • 30

3 Answers3

5

No, you can't "overload" global new - only replace it. However in your replacement you can check for a global flag meaning "new allowed" (and throw an exception if that flag is not set) and change that flag from inside calling code. This won't help against overloaded operator new in classes unless you change each overload to also respect that flag.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • Good, that sounds like what I was thinking of. Do I then need to use malloc to allocate the memory? Will this call the class constructor? – DanS Jul 06 '11 at 07:52
  • @DanS: `operator new()` only allocates memory - C++ will then call the constructor. See this answer on how to implement `operator new()` http://stackoverflow.com/questions/4134195/how-do-i-call-the-original-operator-new-if-i-have-overloaded-it/4134355#4134355 – sharptooth Jul 06 '11 at 07:54
  • @DanS You need to use something other than `operator new` to allocate the memory, and `malloc` is as good as anything else. Note too that if you replace `operator new`, you need to replace `operator delete` as well; if you're using `malloc` to allocate memory, you need to be sure that `operator delete` is using `free`, and not something else. (Although from what you describe, I wouldn't be surprised if an `operator delete` that just aborts wouldn't be appropriate.) – James Kanze Jul 06 '11 at 08:26
2

Not Overloading but Replacing new globally is indeed possible.

The C++ standard has set of predefined new and delete operators. The most commonly used versions are:

void* operator new(std::size_t) throw(std::bad_alloc); 
void  operator delete(void*) throw(); 
void* operator new[](std::size_t) throw(std::bad_alloc);  
void  operator delete[](void*) throw();

The first two versions allocate & deallocate memory for an object, the last two are for array of objects.

If you provide own versions of these is called replacing the ones from the standard library.

If you overload operator new, you should always also overload the matching operator delete, even if will be never called or used by you.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    Note that according to the standard, the default `operator new[]` and `operator delete[]` must forward directly to `operator new` and `operator delete`, so you don't need to replace these unless you want them to do something different. – James Kanze Jul 06 '11 at 08:27
0

you can probably try:

#define new new(your imagination)

and later undefine it depends on your situation

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • The 2 solutions from 2 years ago are much better ideas, if only because they don't break placement new. – MSalters Sep 06 '13 at 10:32
  • use "your imagination", dont break things ;) – Alexandr Poltavsky Sep 15 '13 at 19:44
  • That won't help a bit. Placement new looks like `new (here) Type`. There can be exactly one `()` argument list between `new` and `Type`. Your macro invariably adds that, so it always breaks if another argument list is present. Literally, you can't come up with a single counter-example. – MSalters Sep 15 '13 at 22:25