0+= VARIABLE 如何产生 VARIABLE+1?

发布于 2024-10-20 17:52:31 字数 832 浏览 4 评论 0原文

在更新用于计算 switch 语句变量的计数器时,我遇到了一个奇怪的错误。

int iCount 在循环外被赋值为零,它是用于 while 循环的计数器。

为了更新循环内的计数器,我编写了 iCount+= PackedCount,在本例中,packedCount 为 7。然而,在调试器中,0+=packedCount 导致 PackedCount+1,即 8。这导致数组槽在整个循环中保持未填充状态。

当我将该行更改为 icount= PackedCount+iCount 时,返回了正确的值。

那么,这种行为是 C 所独有的吗?因为我经常在 Java 中这样做,没有出现奇怪的效果。

编辑 - 添加代码片段

#define SKIP   8
#define PACKED 7

int iCount;
iCount=0;

while (iCount < characters-1){  
    for (packedCount=iCount; packedCount< iCount+PACKED; packedCount++){ 
        //ASCII compressor logic goes here
    }
    //iCount+= packedCount; //this produces 8 for 0+packedCount

    //this works
    iCount= iCount+packedCount; //skip the next byte in array, since it was already packed
}

I ran into a strange bug when updating a counter that is used to calculate the variable for a switch statement.

int iCount was assigned zero outside of the loop, and it is the counter used for the while loop.

To update the counter inside the loop, I wrote iCount+= packedCount, where packedCount was 7 in this case. In the debugger, however, 0+=packedCount resulted in packedCount+1, which was 8. That resulted in an array slot remaining unfilled throughout the loop.

When I changed the line to icount= packedCount+iCount, the proper value was returned.

So, is this behavior unique to C, as I do this regularly in Java with no strange effects.

EDIT- Code snippet added

#define SKIP   8
#define PACKED 7

int iCount;
iCount=0;

while (iCount < characters-1){  
    for (packedCount=iCount; packedCount< iCount+PACKED; packedCount++){ 
        //ASCII compressor logic goes here
    }
    //iCount+= packedCount; //this produces 8 for 0+packedCount

    //this works
    iCount= iCount+packedCount; //skip the next byte in array, since it was already packed
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不必你懂 2024-10-27 17:52:31

就编译器而言

iCount += packedCount;
iCount = iCount + packedCount;

是相同的。如果它们产生不同的结果,那么代码中的某些内容会导致 iCount 被丢弃 - 也许是错误的指针引用。

As far as the compilers are concerned

iCount += packedCount;
iCount = iCount + packedCount;

are identical. If they're producing different results, then something in your code is causing iCount to get trashed - a bad pointer reference, perhaps.

一笔一画续写前缘 2024-10-27 17:52:31

试试这个,以及你能想到的其他变体:

#define SKIP   8
#define PACKED 7

int iCount;
iCount=0;

while (iCount < characters-1){  
    for (packedCount=iCount; packedCount< iCount+PACKED; packedCount++){ 
        //ASCII compressor logic goes here
    }
    printf("iCount after the inner loop: %d\n", iCount);             /* DEBUG */
    printf("packedCount after the inner loop: %d\n", packedCount);   /* DEBUG */

    //iCount+= packedCount; //this produces 8 for 0+packedCount

    //this works
    iCount= iCount+packedCount; //skip the next byte in array, since it was already packed

    printf("iCount after update: %d\n", iCount);                     /* DEBUG */
}

Try this, and other variations you can think of:

#define SKIP   8
#define PACKED 7

int iCount;
iCount=0;

while (iCount < characters-1){  
    for (packedCount=iCount; packedCount< iCount+PACKED; packedCount++){ 
        //ASCII compressor logic goes here
    }
    printf("iCount after the inner loop: %d\n", iCount);             /* DEBUG */
    printf("packedCount after the inner loop: %d\n", packedCount);   /* DEBUG */

    //iCount+= packedCount; //this produces 8 for 0+packedCount

    //this works
    iCount= iCount+packedCount; //skip the next byte in array, since it was already packed

    printf("iCount after update: %d\n", iCount);                     /* DEBUG */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文