[英]unsigned int* assignment changing the target unsigned int
Ok this must be something dumb. 好的,这一定是愚蠢的。 I ran into this when moving some code over and figured I had made a typo or was failing to use the debugger correctly.
我在移动一些代码时遇到了这个问题,并发现我输入错误或无法正确使用调试器。 As a sanity check I created this test case but it still appears to be failing.
作为健全性检查,我创建了这个测试用例,但它似乎仍然失败。
unsigned int vtxIdx = 0;
unsigned int* tgtIdx;
NSLog(@"Init %d", vtxIdx);
tgtIdx = &vtxIdx;
NSLog(@"After %d", vtxIdx);
float* pVtx = new float[1000*3];
NSLog(@"After more %d", vtxIdx);
Output: 输出:
2011-03-24 09:59:23.494 Game-iOS[] Init 0
2011-03-24 09:59:25.677 Game-iOS[] After 4
2011-03-24 09:59:31.828 Game-iOS[] After more 12
Edit: 编辑:
What initially tipped me off was I was seeing weird values in the XCode variable window. 最初提示我的是,我在XCode变量窗口中看到了奇怪的值。 So I don't think it is an NSLog issue as I see those same value in XCode.
所以我认为这不是NSLog问题,因为我在XCode中看到了相同的值。 Watching vtxIdx in the debugger it appears to increase by 4 with each instruction.
看着调试器中的vtxIdx,每条指令似乎增加了4。
All of this code is in an .mm files if that matters. 如果重要的话,所有这些代码都位于.mm文件中。
For your second output statement, do you get the same thing if you print the values of both vtxIdx
and *tgtIdx
? 对于第二个输出语句,如果同时
vtxIdx
和*tgtIdx
的值,会得到相同的*tgtIdx
吗?
Does anything change if you initialize tgtIdx = NULL;
如果您初始化
tgtIdx = NULL;
则一切都tgtIdx = NULL;
改变tgtIdx = NULL;
? ?
If you run each NSLog
function twice in a row with the exact same arguments, do they output the same thing each time? 如果您使用完全相同的参数连续两次运行每个
NSLog
函数,它们每次都会输出相同的内容吗?
You mentioned a debugger in your question. 您在问题中提到了调试器。 What did you see when you single-stepped through this program?
单步执行此程序时,您看到了什么?
Edit: A few more ideas. 编辑:更多的想法。
What happens if you comment out the tgtIdx = &vtxIdx;
如果注释掉
tgtIdx = &vtxIdx;
会发生什么? line? 线?
What happens if you add the line unsigned int dummy;
如果添加行
unsigned int dummy;
会发生什么情况unsigned int dummy;
before your existing variable declarations? 在您现有的变量声明之前?
Since something seems to be changing the variable behind your back, my first thoughts are a memory corruption issue (some other code is writing over your variable unintentionally) or a linkage problem (the linker saw your variable and another variable with the same name, thought they were the same object, and merged them together). 由于某些事情似乎正在改变您背后的变量,因此我首先想到的是内存损坏问题(其他一些代码无意间写了您的变量)或链接问题(链接器看到了您的变量和另一个同名变量,它们是同一个对象,并将它们合并在一起)。 Adding the dummy variable declaration should help indicate if the problem is a memory corruption problem, and renaming the variables should test for any possible name resolution conflicts.
添加虚拟变量声明应有助于指示问题是否是内存损坏问题,并且重命名变量应测试任何可能的名称解析冲突。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.