简体   繁体   English

unsigned int *赋值,更改目标unsigned int

[英]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.

相关问题 如何将unsigned int转换为NSString? - how to convert an unsigned int to NSString? 在Objective-C中将unsigned char *转换为int * - Convert unsigned char * to int * in Objective-C 如何从int转换为unsigned short int-iPhone - How do I convert from int to unsigned short int - iPhone iPhone上“unsigned int”和“int”之间的性能有何不同? - Is there a difference in term of performance between “unsigned int” and “int” on the IPhone? 格式指定 'unsigned int' 但参数类型为 'char*' - Format specifies 'unsigned int' but the argument has type 'char*' 如何将unsigned int值转换为255.255.255.255之类的IP地址? - How to convert an unsigned int value into an IP address like 255.255.255.255? 值转换问题:隐式转换失去整数精度:'size_t'(又名'unsigned long')到'int' - Value Conversion issue: Implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' 语义问题:指向整数转换的指针不兼容,将'NSUInteger *'(又名'unsigned int *')发送到'NSUInteger'类型的参数 - Semantic Issue: Incompatible pointer to integer conversion sending 'NSUInteger *' (aka 'unsigned int *') to parameter of type 'NSUInteger' 将NSString强制转换为无符号字符* - Casting NSString to unsigned char * 分发未签名的应用程序iPhone - Distribute unsigned app iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM