C 数组更改导致变量修改

发布于 2024-10-04 21:49:57 字数 1164 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时常饿 2024-10-11 21:49:57

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

Guessing that currentPrimeCandidate is greater than the maximum index of myArray, and you're overwriting innerSieve (which likely follows myArray on the stack).

离去的眼神 2024-10-11 21:49:57

@ruslik 在评论中提到了这一点。问题在于这一行:

if ((nextPrimeCandidate^2) > SIEVELIMIT ) break;

在 C 中,^ 运算符不是幂运算符,它是按位异或运算符。您迭代的次数比预期的次数多,这会导致数组索引越界错误,因此您会覆盖随机内存并得到奇怪的结果。

C 中没有幂运算符(尽管有 pow 功能)。由于您只是对数字进行平方,因此最简单的解决方法是将数字乘以自身:

if ((nextPrimeCandidate * nextPrimeCandidate) > SIEVELIMIT ) break;

@ruslik hit on it in the comment. The problem is this line:

if ((nextPrimeCandidate^2) > SIEVELIMIT ) break;

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:

if ((nextPrimeCandidate * nextPrimeCandidate) > SIEVELIMIT ) break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文