退出预处理器块时整数值发生变化
我有一段代码,其中变量似乎在预处理器代码块的末尾发生了变化。
int initialKeyCount;
#if(DEBUG)
// int initialKeyCount = _root.CountAllKeys();
initialKeyCount = 20000;
#endif
currNode = currNode.EnsureDegreeKeysPresent(parent); //initialKeyCount = 19969 here
#if(DEBUG)
int currentKeyCount = _root.CountAllKeys();
Debug.Assert(initialKeyCount == currentKeyCount,
string.Format("EnsureDegreeNodesPresent changed the node count from {0} to {1}.", initialKeyCount, currentKeyCount));
#endif
当在调试器中执行此操作时,假定分配了 20000 之后,initialKeyCount = 19969。我对此进行了一些尝试,发现在第一个预处理器块内对initialKeyCount 的分配是正确的,但是一旦代码离开第一个预处理器block 值神奇地更改为 19969。
无论变量是在第一个预处理器块内部还是外部声明,此行为都是相同的。 该值在第二个预处理器块内保留为 19969。
在预处理器块中进行的分配在该块之外是否未定义? 这似乎是错误的,但似乎正是这里发生的事情。
I have a chunk of code where it appears that a variable is changing at the end of a pre-processor block of code.
int initialKeyCount;
#if(DEBUG)
// int initialKeyCount = _root.CountAllKeys();
initialKeyCount = 20000;
#endif
currNode = currNode.EnsureDegreeKeysPresent(parent); //initialKeyCount = 19969 here
#if(DEBUG)
int currentKeyCount = _root.CountAllKeys();
Debug.Assert(initialKeyCount == currentKeyCount,
string.Format("EnsureDegreeNodesPresent changed the node count from {0} to {1}.", initialKeyCount, currentKeyCount));
#endif
When executing this in the debugger initialKeyCount = 19969 after supposedly assigning 20000. I have played around with this a bit and found that assignment to initialKeyCount is correct inside the first pre-processor block, but as soon as the code leaves the first pre-processor block the value magically changes to 19969.
This behavior is the same regardless of whether the variable is declared inside or outside the first pre-processor block. The value remains 19969 inside the second pre-processor block.
Are assignments made in a pre-processor block undefined outside of that block? That seems wrong but appears to be what is happening here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这种行为听起来很像调试器正在运行与您正在编辑的源代码不匹配的代码。 您是否绝对确定您的源代码更改会一直影响到您正在运行的代码?
预处理器块与语言语法无关。 因此,您说预处理器块不会影响变量定义的范围是正确的。
This sort of behaviour sounds very much like the debugger is running code that doesn't match the source code you are editing. Are you absolutely sure that your source changes are making it all the way to the code you're running?
The preprocessor blocks are unrelated to the language syntax. So, you're correct in saying that preprocessor blocks do not affect the scope of variable definitions.
听起来 Visual Studio 很困惑。 按顺序尝试这些步骤
我以前在我工作的一家 IT 公司每周都会看到一次这样的情况。 当您拥有同一项目的多个副本时,通常会发生这种情况,但即使没有,我也看到过这种情况。
Sounds like Visual Studio got confused. Try these steps in order
I used to see this once a week at an IT company I worked for. It usually happens when you have multiple copies of the same project, but I've seen it even without that.
我同意 Greg Hewgill 的观点——我以前见过这种事情。
另外,找到调试器正在使用的程序集并使用 Reflector 打开它。 反汇编应该可以让您更好地了解实际发生的情况。
I agree with Greg Hewgill - I have seen this sort of thing before.
Also, find the assembly the debugger is using and open it with Reflector. The dissassembly should give you a better idea of what is actually happening.
当遇到这样的事情时,从汇编层面来看。
虽然现在你几乎不会编写汇编代码,但你需要了解它才能追查出这样的谜团。
When faced with something like this, look at it at the assembly level.
While assembly is something you will almost never code in these days one NEEDS to know it to trace down mysteries like this.
事情变得越来越奇怪。 我采纳了上述建议,并使用 Reflector 检查了代码以及调试器提供的反汇编,两者看起来都如您所期望的那样。 我稍微修改了代码,以清楚地显示变量中的“神奇”变化。
新代码是
上面的反汇编是
使用内存窗口我观察了 ebp-0x50 处的值
当 IP
为 00000094 时,值为 0x0
在 0000009b 处,值为 0x4e20
在 0000009e 处,值为 0x4e21
在 0000009f 处,值是 0x4e01
我承认自从我编写任何汇编代码以来已经很长时间了,但我非常有信心 nop 不应该写入内存。 :)
显然,有些代码正在执行,但调试器没有显示。 有谁知道我使用预处理器的方式是否导致了这种情况,或者这只是一个错误?
Things get stranger and stranger. I took the above suggestions and examined the code with Reflector and the disassembly provided by the debugger, both look as you would expect. I modified the code slightly to clearly show the "magic" change in the variable.
The new code is
The disassembly for the above is
Using the memory window I watched the value at ebp-0x50
When IP is
at 00000094 the value is 0x0
at 0000009b the value is 0x4e20
at 0000009e the value is 0x4e21
at 0000009f the value is 0x4e01
I'll admit it's been a LONG time since I wrote any assembly code, but I'm pretty confident that nop shouldn't be writing to memory. :)
Obviously some code is executing that the debugger is not displaying. Does anyone know if there is something about how I've used the pre-processor that causes this, or is this simply a bug?