C数组变化导致变量修改

标签 c arrays gcc ubuntu-9.04

我正在尝试使用 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 调试工具检查时,似乎寄存器中发生了一些有趣的事情,但我不太确定我应该寻找什么。

如果您需要整个代码 list ,可以提供。(在代码中尚未初始化的特定变量在 innerSieve 变量达到 32 的精确点进行初始化)

任何帮助将不胜感激。

最佳答案

猜测 currentPrimeCandidate 大于 myArray 的最大索引,并且您正在覆盖 innerSieve(可能跟在 myArray 之后 在堆栈上)。

关于C数组变化导致变量修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4309546/

相关文章:

C : Sum of reverse numbers

c - 为什么我的 fork() 没有输出任何东西?

javascript - 如何更好地构建 JSON 中的数组数组

javascript - 向页面添加大量元素时性能不佳

debugging - 从 elf 二进制文件中提取调试符号信息

gcc - 如何防止 Qt Creator 为同一个库生成多个 .so 文件?

C 编程 - 如何仅使用循环打印带有小数部分的数字?

c++ - 为什么 C 中字 rune 字的大小与 C++ 中的不同

Java - 计算 2D boolean 数组中剩余的 false

python - 错误: "Undefined Reference to" - Compile C extension for Python using Anaconda