[cfe-dev] Is valid optimization: dropping new[]

Dmitri Shubin sbn at tbricks.com
Wed Feb 6 00:19:07 PST 2013

On 02/06/13 11:49, John McCall wrote:
> The optimizer is making not-strictly-standard assumptions about the
> behavior of global operator new[] and the merits of intentionally triggering
> an out-of-memory condition with a leak vs. promoting leaked allocations
> to the stack.  You can disable these assumptions with -fno-builtin, avoid
> them by compiling at -O0, or work around them by assigning each 'p' in
> turn to a volatile global variable, which will stop the compiler from realizing
> that they leak.

Yes, making p to be 'static volatile char *' fixed the problem, thank you!

Do I understand you right that the compiler did the following:
1. replaced 'new char[1024]' with smth like 'alloca(1024)'
2. since alloca() cannot fail it removed try/catch and if() statements.
3. since the result isn't used 'alloca()' call was removed also.

Well for 1024 bytes I think it's reasonable, but I can see the same 
behavior when changing 1024 to smth bigger, like 2^30 which makes this 
transformation wrong from my point of view.

BTW I used to think that -O2 is more or less safe optimization level.

More information about the cfe-dev mailing list