Explicitly initialize DWORD to 1, but debugger shows wildly out of range value

元气小坏坏 提交于 2019-11-27 07:13:38

问题


I'm a bit flabberghasted with this issue - in debug mode this issue doesn't come up at all, but in release mode it feels like there's almost nothing I can do to fix it.

Here's a screenshot of what's going on inside VS2013:

I initialize nResult to 1L, and the internal value is shown as 3422785012. In Debug mode, this value is indeed 1L.

Any ideas as to why the displayed value isn't even close to the initial value I set for nResult? I've tried playing with compiler settings - in Debug mode this code runs fine.


回答1:


Your code is more than likely optimized. This means that the compiler has moved and eliminated code in a way that your source code does not match what the compiler has produced.

Either debug unoptimized code, or debug optimized code with the full knowledge that the source may not match the actual instructions being performed.

If you want to debug a release build but see the correct values in the debugger, go to the Properties of your project in Visual Studio, go to the C/C++ section, and disable the optimizations. Rebuild.




回答2:


Debugging release code is quite difficult as the compiler will reorder, remove, inline and rearrange things.

The only reliable way to debug a "release" build is to leave all release options enabled, but disable code optimizations. Or you could just disable optimizations in any modules you want full debugging info from. I've seen this done in the past when a bug was impossible to track down in a fully optimized released x64 build, as x64 clobbers the parameters far more often than x86 (when looking at crash dumps).



来源:https://stackoverflow.com/questions/23348983/explicitly-initialize-dword-to-1-but-debugger-shows-wildly-out-of-range-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!