[cfe-dev] Can replacement delete[] have side-effects?

Krzysztof Parzyszek via cfe-dev cfe-dev at lists.llvm.org
Tue Aug 29 12:56:25 PDT 2017


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";
}
--------------

$ clang++ -O2 new.cc -mllvm -inline-threshold=10
$ ./a.out
Failed

$ clang++ -O2 new.cc -mllvm -inline-threshold=100
$ ./a.out


-Krzysztof



-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, 
hosted by The Linux Foundation



More information about the cfe-dev mailing list