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. So I don't think it is an NSLog issue as I see those same value in XCode. Watching vtxIdx in the debugger it appears to increase by 4 with each instruction.
All of this code is in an .mm files if that matters.
For your second output statement, do you get the same thing if you print the values of both vtxIdx
and *tgtIdx
?
Does anything change if you initialize 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?
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;
line?
What happens if you add the line 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.
精彩评论