C 数组更改导致变量修改
我正在尝试使用 C 编程语言修改数组中的值,但这个看似简单的操作似乎遇到了障碍。请参阅下面的代码片段:
while(1) {
printf("Current prime candidate is %i\n",nextPrimeCandidate);
int innerSieve;//=2;
int currentPrimeCandidate=0;
for (innerSieve=2;innerSieve<SIEVELIMIT;innerSieve++) {
currentPrimeCandidate = nextPrimeCandidate * innerSieve;
//printf("Inner Sieve is b4 funny place %i,%i\n",innerSieve,currentPrimeCandidate);
//initArray[currentPrimeCandidate]=5;
//VERY UNIQUE LINE
myArray[currentPrimeCandidate] = 0;
//printf("Inner Sieve after funny place is %i,%i \n",innerSieve,currentPrimeCandidate);
}
nextPrimeCandidate=getNextPrimeCandidate(myArray,++nextPrimeCandidate);
if ((nextPrimeCandidate^2) > SIEVELIMIT ) break;
}
问题在于用 VERY UNIQUE LINE
注释突出显示的行。由于某种原因,当innerSieve变量达到33并到达该行时,它将innerSieve变量的内容设置为该行的值(当前为0),并且基本上强制循环进入无限循环(SIEVELIMIT
变量设置为 50)。当我使用 Eclipse 调试工具检查时,寄存器中似乎发生了一些有趣的事情,但我不太确定我应该寻找什么。
如果您需要整个代码清单,可以提供。(在 innerSieve 变量达到 32 的精确点初始化的代码中尚未初始化的特定变量)
任何帮助将不胜感激。
I am trying to modify a value in an array using the C programming language and I seem to be hitting a blank wall with this seemingly easy operation. Please see code snippet below:
while(1) {
printf("Current prime candidate is %i\n",nextPrimeCandidate);
int innerSieve;//=2;
int currentPrimeCandidate=0;
for (innerSieve=2;innerSieve<SIEVELIMIT;innerSieve++) {
currentPrimeCandidate = nextPrimeCandidate * innerSieve;
//printf("Inner Sieve is b4 funny place %i,%i\n",innerSieve,currentPrimeCandidate);
//initArray[currentPrimeCandidate]=5;
//VERY UNIQUE LINE
myArray[currentPrimeCandidate] = 0;
//printf("Inner Sieve after funny place is %i,%i \n",innerSieve,currentPrimeCandidate);
}
nextPrimeCandidate=getNextPrimeCandidate(myArray,++nextPrimeCandidate);
if ((nextPrimeCandidate^2) > SIEVELIMIT ) break;
}
The problem is with the line highlighted with the VERY UNIQUE LINE
comment. For some reason, when the innerSieve variable reaches 33 and gets to that line, it sets the contents of the innerSieve variable to the value of that line ( which currently is 0) and basically forces the loop into an infinite loop( the SIEVELIMIT
variable is set at 50). It seems that there is some funny stuff going on in the registers when I checked using the Eclipse Debug facility but I am not too sure what I should be looking for.
If you need the whole code listing, this can be provided.( with a particular variable which is not yet initialised in the code being initialised at the precise point that the innerSieve variable hits 32)
Any help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
猜测
currentPrimeCandidate
大于myArray
的最大索引,并且您正在覆盖innerSieve
(可能遵循myArray
> 在堆栈上)。Guessing that
currentPrimeCandidate
is greater than the maximum index ofmyArray
, and you're overwritinginnerSieve
(which likely followsmyArray
on the stack).@ruslik 在评论中提到了这一点。问题在于这一行:
在 C 中,
^
运算符不是幂运算符,它是按位异或运算符。您迭代的次数比预期的次数多,这会导致数组索引越界错误,因此您会覆盖随机内存并得到奇怪的结果。C 中没有幂运算符(尽管有
pow
功能)。由于您只是对数字进行平方,因此最简单的解决方法是将数字乘以自身:@ruslik hit on it in the comment. The problem is this line:
In C, the
^
operator is not the power operator, it is the bitwise xor operator. You're iterating far too many times than you intend, which is resulting in an array-index-out-of-bounds error, so you're overwriting random memory and getting strange results.There is no power operator in C (though there is the
pow
function). Since you're just squaring a number, the simplest fix is to multiply the number by itself: