tags:

views:

24

answers:

2

I'm using Visual Studio 2008 Pro programming in c++. When I press the run button in debugging mode, are any compiler optimizations applied to the program by default?

+1  A: 

The debugger will by default be running a debug build, which won't have optimizations turned on.

If optimizations are enabled, you may notice that "Step" and "Next" sometimes appear to cause the program flow to jump around. This is because the compiler sometimes re-order instructions and the debugger is doing it's best.

dicroce
Thanks. A second to the question, is it possible to turn on optimizations during a debugging run? If so, how?
Faken
Some optimizations may work, but some will totally destroy your ability to debug. As for whether you can enable them when debugging in VS, I don't know for sure, but I suspect that you probably can.
dicroce
I want to enable optimizations for some speed tests (thus breakpoints wont be in there). I guess its just for convenience that i can simply press the run button and get a time.
Faken
A: 

I suppose it depends on what you'd classify as optimizations, but mostly no. Just for example, recent versions of VS do apply the (anonymous) return value optimization, at least in some cases, even with optimization disabled (/O0) as is normal for a debug build.

If you want to debug optimized code, it's usually easiest to switch to a release build, and then tell it to generate debug info. In theory you can turn on optimization in a debug build, but you have to change more switches to do it.

Jerry Coffin