views:

46

answers:

2

Hi, I am using working on a c++ application in Visual studio 2008 I have built my project with Maximize speed(/o2) (From Properties -> configuration properties -> c/c++ -> optimization -> optimization) Unable to watch the variable values while debugging the code. Pls help.

+3  A: 

This is normal. Once you activate optimizations, the compiler pretty much ignores all your variable requests and tries to do what it thinks is best.

There's a reason why the debug builds have no optimizations enabled :)

Edit: You might have some luck with enabling or disabling optimizations for specific bits of code. Try this link: http://msdn.microsoft.com/en-us/library/chh3fb0k(VS.80).aspx

Blindy
So, is it not possible to optimize a debug build ? What if i want to do faster debugging ?If its not allowing optimization in debug build, when user sets optimization, does it give any warning ?
bjskishore123
No warning, variables just sorta "dissapear" as they're moved/merged around.
Blindy
@bjskishore123: you can still debug a release build. But you have to live with your code being mangled somewhat, because that's what optimizations *do*. If an optimization removes a variable from the code, you obviously can't watch it in the debugger. You have a tradeoff between "sticking exactly to the code I wrote" versus "transforming my code into something more efficient", where the latter means there will be discrepancies between what the source code says (for example that a variable exists, or that line N is executed before line N+1) and what actually happens when you run or debugthe app
jalf
+2  A: 

Sometimes resorting to the "disassembly view" of the C++ code and stepping through the optimized release x86 give you an idea of what's going on. Pay special attention to function calls (many times those cannot be optimized out), and code around those calls - before and after, and register usage (many times variables are optimized into hardware registers).

Regardless of whether this works or not, you learn a little bit about x86 assembly, compiler optimization, and how C++ is actually implemented!

franji1