Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why #pragma optimize("", off)

I'm reviewing a C++ MFC project. At the beginning of some of the files there is this line:

#pragma optimize("", off)

I get that this turns optimization off for all following functions. But what would the motivation typically be for doing so?

like image 600
Stokke Avatar asked Mar 13 '15 13:03

Stokke


3 Answers

I have used this exclusively to get better debug information in a particular set of code with the rest of the application is compiled with the optimization on. This is very useful when running with a full debug build is impossible due to the performance requirement of your application.

like image 163
Ray Avatar answered Oct 22 '22 01:10

Ray


I've seen production code which is correct but so complicated that it confuses the optimiser into producing incorrect output. This could be the reason to turn optimisations off.

However, I'd consider it much more likely that the code is simply buggy, having Undefined Behaviour. The optimiser exposes that and leads to incorrect runtime behaviour or crashes. Without optimisations, the code happens to "work." And rather than find and remove the underlying problem, someone "fixed" it by disabling optimisations and leaving it at that.

Of course, this is about as fragile and workarounds can get. New hardware, new OS patch, new compiler patch, any of these can break such a "fix."

Even if the pragma is there for the first reason, it should be heavily documented.

like image 41
Angew is no longer proud of SO Avatar answered Oct 22 '22 00:10

Angew is no longer proud of SO


Another alternative reason for these to be in a code base... Its an accident.

This is a very handy tool for turning off the optimizer on a specific file whilst debugging - as Ray mentioned above.

If changelists are not reviewed carefully before committing, it is very easy for these lines to make their way into codebases, simply because they were 'accidentally' still there when other changes were committed.

like image 15
JoshG Avatar answered Oct 22 '22 01:10

JoshG