Integer value changes when exiting the preprocessor block
I have a piece of code where the variable appears to be changing at the end of the cpu block in front of the cpu.
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 doing this in the debugger, initialKeyCount = 19969 after an assumed assignment of 20000. I played around with this a bit and found that the initialKeyCount assignment is correct inside the first preprocessor block, but as soon as the code leaves the first preprocessor block, the value magically changes to 1996.
This behavior is the same regardless of whether the variable is declared inside or outside the first block of the pre-processor. The value remains at 19969 in the second pre-processor block.
Are the assignments made in the pre-processor block undefined outside this block? It seems wrong, but it looks like what is happening here.
source to share
Looks like Visual Studio got confused. Try the following:
- Use the "Clear" command
- Restart Visual Studio
- Remove every DLL and EXE that even remotely looks like your program.
- Double check each BIN and OBJ folder to see if you missed anything.
- Find the entire hard drive for any DLL and EXE that even remotely looks like your program and removes them.
I saw this once a week at the IT company I worked for. This usually happens when you have multiple copies of the same project, but I've seen it even without it.
source to share
This behavior is very similar to the debugger running code that does not match the source code you are editing. Are you absolutely sure that your original changes are completely consistent with the code you are using?
The preprocessor blocks are not related to the syntax of the language. So, you are right when you say that preprocessor blocks do not affect the scope of variables.
source to share
Things become unfamiliar and unfamiliar. I accepted the above suggestions and looked at the code with Reflector and the disassembly provided by the debugger, both look what you'd expect. I modified the code a bit to clearly show the "magic" change in the variable.
New code
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
Disassembly for the above
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 looked at the value on ebp-0x50 When IP
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 the assembly code, but I'm pretty sure the nop shouldn't be written to memory. :)
Obviously, some code is doing what the debugger doesn't display. Does anyone know if there is something about how I was using the preprocessor that calls this, or is it just a bug?
source to share