The code generated to call operator new[] causes a segmentation fault when operator new[] returns 0 to indicate memory exhaustion. The code generated by GCC that calls operator new[] (with -fcheck-new) increments the memory pointer returned by operator new[] by 4 *before* comparing it to 0 to see if it should call the ctor. The segmentation fault occurs when the constructor is called. ---- sample.cpp ---- // g++ -g -fcheck-new -fno-exceptions sample.cpp -o sample #include <stdlib.h> class foo { public: int v; foo(){ v = 0; } ~foo() {} void* operator new[](size_t size) { return 0; // simulated memory failure } void operator delete[](void* p, size_t size) { } }; main() { foo *p = new foo[2]; if (p) delete [] p; return 0; } ---- EOF ---- How-To-Repeat: $ g++ -fcheck-new -fno-exceptions sample.cpp -o sample $ ./sample Segmentation fault
I can confirm this with 3.3, but it seems fixed in 3.4. As -fcheck-new is not a frequently used option, I am not certain this will be fixed in 3.3.3...
This is a regression from 2.95.3.
Created attachment 5309 [details] Sample without -fcheck-new This also happens without -fcheck-new, when using `operator new[] () throw()'.
I can confirm this with the example without -fcheck-new and it works on them mainline.
The fix went into the mainline between 20030410 and 20030413.
The patch to fix this is here: <http://gcc.gnu.org/ml/gcc-patches/2003-04/msg00948.html>. Mark it looks like your patch for 3.3 did not fix all the problems, assigning it to you as your patch for the mainline fixes it there.
It's not practical to backport the mainline changes to GCC 3.3.3. I don't think that it's worth spending effort to fix this bug for GCC 3.3.3, so I've removed my name from the assigned field. However, if I caused this regression and Gaby thinks it's imperative that this bug be fixed, I will see if I can fix the problem.
Subject: Re: [3.3 Regression] bad code generated to call operator new[] when -fcheck-new "mmitchel at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes: | It's not practical to backport the mainline changes to GCC 3.3.3. So, let's close it as WONTFIX for 3.3.x Thanks, -- Gaby
Not to be fixed for 3.3.3.
*** Bug 19975 has been marked as a duplicate of this bug. ***