退出预处理器块时整数值发生变化

发布于 2024-07-11 10:50:35 字数 805 浏览 8 评论 0原文

我有一段代码,其中变量似乎在预处理器代码块的末尾发生了变化。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太傻旳人生 2024-07-18 10:50:35

这种行为听起来很像调试器正在运行与您正在编辑的源代码不匹配的代码。 您是否绝对确定您的源代码更改会一直影响到您正在运行的代码?

预处理器块与语言语法无关。 因此,您说预处理器块不会影响变量定义的范围是正确的。

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.

彼岸花ソ最美的依靠 2024-07-18 10:50:35

听起来 Visual Studio 很困惑。 按顺序尝试这些步骤

  1. 使用 Clean 命令
  2. 重新启动 Visual Studio
  3. 删除每个 DLL 和 EXE,甚至远程看起来像您的程序
  4. 仔细检查每个 BIN 和 OBJ 文件夹,看看是否遗漏了任何内容。
  5. 在你的整个硬盘上搜索任何看起来像你的程序的 DLL 和 EXE,然后将它们也删除。

我以前在我工作的一家 IT 公司每周都会看到一次这样的情况。 当您拥有同一项目的多个副本时,通常会发生这种情况,但即使没有,我也看到过这种情况。

Sounds like Visual Studio got confused. Try these steps in order

  1. Use the Clean command
  2. Restart Visual Studio
  3. Delete every DLL and EXE that even remotely looks like your program
  4. Double check every BIN and OBJ folder to see if you missed anything.
  5. Search your whole hard drive for any DLL and EXE that even remotely looks like your program and delete them too

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.

心在旅行 2024-07-18 10:50:35

我同意 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.

回心转意 2024-07-18 10:50:35

当遇到这样的事情时,从汇编层面来看。

虽然现在你几乎不会编写汇编代码,但你需要了解它才能追查出这样的谜团。

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.

趁微风不噪 2024-07-18 10:50:35

事情变得越来越奇怪。 我采纳了上述建议,并使用 Reflector 检查了代码以及调试器提供的反汇编,两者看起来都如您所期望的那样。 我稍微修改了代码,以清楚地显示变量中的“神奇”变化。

新代码是

int initialKeyCount;
#if(DEBUG)
//          int initialKeyCount = _root.CountAllKeys();
      initialKeyCount = 20000;
      initialKeyCount++;
      initialKeyCount = initialKeyCount;
#endif
      currNode = currNode.EnsureDegreeKeysPresent(parent);
#if(DEBUG)
      int currentKeyCount = _root.CountAllKeys();
      Debug.Assert(initialKeyCount == currentKeyCount,
               string.Format("EnsureDegreeNodesPresent changed the node count from {0} to {1}.", initialKeyCount, currentKeyCount));
#endif

上面的反汇编是

int initialKeyCount;
#if(DEBUG)
//          int initialKeyCount = _root.CountAllKeys();
      initialKeyCount = 20000;
00000094  mov         dword ptr [ebp-50h],4E20h 
      initialKeyCount++;
0000009b  inc         dword ptr [ebp-50h] 
      initialKeyCount = initialKeyCount;
0000009e  nop              
#endif
      currNode = currNode.EnsureDegreeKeysPresent(parent);
0000009f  mov         edx,dword ptr [ebp-48h] 
...

使用内存窗口我观察了 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

int initialKeyCount;
#if(DEBUG)
//          int initialKeyCount = _root.CountAllKeys();
      initialKeyCount = 20000;
      initialKeyCount++;
      initialKeyCount = initialKeyCount;
#endif
      currNode = currNode.EnsureDegreeKeysPresent(parent);
#if(DEBUG)
      int currentKeyCount = _root.CountAllKeys();
      Debug.Assert(initialKeyCount == currentKeyCount,
               string.Format("EnsureDegreeNodesPresent changed the node count from {0} to {1}.", initialKeyCount, currentKeyCount));
#endif

The disassembly for the above is

int initialKeyCount;
#if(DEBUG)
//          int initialKeyCount = _root.CountAllKeys();
      initialKeyCount = 20000;
00000094  mov         dword ptr [ebp-50h],4E20h 
      initialKeyCount++;
0000009b  inc         dword ptr [ebp-50h] 
      initialKeyCount = initialKeyCount;
0000009e  nop              
#endif
      currNode = currNode.EnsureDegreeKeysPresent(parent);
0000009f  mov         edx,dword ptr [ebp-48h] 
...

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文