Can replacement delete[] have side-effects?

Other than deallocating memory, that is?

This testcase will either print nothing, or print "Failed", depending on how much inlining takes place. The replacement delete operator is treated as the usual deallocation function and is removed despite having an observable side-effect other than deallocation of memory.

Is the testcase correct, or is this a bug in LLVM?

--- new.cc ---
#include <new>
#include <cstdlib>
#include <cstddef>
#include <iostream>

using namespace std;

static bool global = false;

void* operator new(::size_t size) throw(bad_alloc) {
   return ::malloc(size);
}

void operator delete(void *ptr) throw() {
   global = true;
   if (ptr != NULL)
     ::free(ptr);
}

int main(void) {
   int *p = new int[2];
   global = false;
   delete p;
   if (!global)
     cout << "Failed\n";
}

A replacement global (de)allocation function can have side effects. But the C++ standard does not require a new/delete expression to actually call the (de)allocation function, and is permitted to provide the storage in another way instead.

For the full details, see http://eel.is/c++draft/expr.new#10

If you want to guarantee that your functions are called (eg when testing an allocator implementation), you can call them directly (“operator new(size)”).

That explains it. Thanks, both of you.

-Krzysztof