<div dir="auto"><div><div class="gmail_extra"><div class="gmail_quote">On 29 Aug 2017 12:56, "Krzysztof Parzyszek via cfe-dev" <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Other than deallocating memory, that is?<br>
<br>
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.<br>
<br>
Is the testcase correct, or is this a bug in LLVM?<br></blockquote></div></div></div><div dir="auto"><br></div><div dir="auto">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.</div><div dir="auto"><br></div><div dir="auto">For the full details, see <a href="http://eel.is/c++draft/expr.new#10">http://eel.is/c++draft/expr.new#10</a></div><div dir="auto"><br></div><div dir="auto">If you want to guarantee that your functions are called (eg when testing an allocator implementation), you can call them directly ("operator new(size)").</div><div dir="auto"><br></div><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
--- new.cc ---<br>
#include <new><br>
#include <cstdlib><br>
#include <cstddef><br>
#include <iostream><br>
<br>
using namespace std;<br>
<br>
static bool global = false;<br>
<br>
void* operator new[](::size_t size) throw(bad_alloc) {<br>
return ::malloc(size);<br>
}<br>
<br>
void operator delete[](void *ptr) throw() {<br>
global = true;<br>
if (ptr != NULL)<br>
::free(ptr);<br>
}<br>
<br>
int main(void) {<br>
int *p = new int[2];<br>
global = false;<br>
delete[] p;<br>
if (!global)<br>
cout << "Failed\n";<br>
}<br>
--------------<br>
<br>
$ clang++ -O2 new.cc -mllvm -inline-threshold=10<br>
$ ./a.out<br>
Failed<br>
<br>
$ clang++ -O2 new.cc -mllvm -inline-threshold=100<br>
$ ./a.out<font color="#888888"><br>
<br>
<br>
-Krzysztof<br>
<br>
<br>
<br>
-- <br>
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation<br>
______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-dev</a><br>
</font></blockquote></div><br></div></div></div>