意外的整数换行

发布于 2024-10-22 10:45:34 字数 671 浏览 1 评论 0原文

我遇到一个整数意外回绕到最小值的问题。

在换行到 -858993460 之前,该整数的值为 15。

这里是导致此问题的代码:

while(ArrayLocation2 < EmpArray2Size)
{
    GivenEmployees[(*EmployeeSize++)] = curr2;

    prev2 = curr2;
    if(ArrayLocation2 < EmpArray2Size)
    {
        curr1 = EmpArray2[ArrayLocation2];
    }
    ArrayLocation2++;

    if((ArrayLocation2 >= EmpArray2Size) || (prev2.HourlyRate > curr2.HourlyRate))
    {
        subFiles++;
    }
}

如果我手动更改它所需的值(16、17、 18 等)它按预期工作。

Size 声明为 int Size = 21; 并作为 &Size 传递到其当前方法(如果有差异)。

为什么会发生这种情况?

I am having an issue with an integer wrapping around to its minimum value unexpectedly.

The value of the integer is 15 before it wraps to -858993460.

Here is the code that is causing this issue:

while(ArrayLocation2 < EmpArray2Size)
{
    GivenEmployees[(*EmployeeSize++)] = curr2;

    prev2 = curr2;
    if(ArrayLocation2 < EmpArray2Size)
    {
        curr1 = EmpArray2[ArrayLocation2];
    }
    ArrayLocation2++;

    if((ArrayLocation2 >= EmpArray2Size) || (prev2.HourlyRate > curr2.HourlyRate))
    {
        subFiles++;
    }
}

If I manually change the values that it needs (16, 17, 18, etc) it works as expected.

Size is declared as int Size = 21; and passed into its current method as &Size if it makes a difference.

Why is this exactly happening?

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

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

发布评论

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

评论(3

如歌彻婉言 2024-10-29 10:45:34

问题是你正在增加指针 - 它最终指向随机区域。

您可能打算写:

GivenEmployees[(*EmployeeSize)++] = cur2;

这里括号是必需的;它们在原作中是不必要的。


来自评论:

包装的整数是“EmployeeSize”,并按照我在OP中描述的方式进行声明。

(除了它在原始OP中被称为“大小”。)但是,它似乎是:

void this_function(..., int *EmployeeSize, ...)
{
    ...code...
}

The problem is that you are incrementing the pointer - and it ends up pointing into random territory.

You probably intended to write:

GivenEmployees[(*EmployeeSize)++] = cur2;

The parentheses are necessary here; they are unnecessary in the original.


From the comments:

The integer that is wrapping is "EmployeeSize" and is declared as I've described in the OP.

(Except that it is called 'Size' in the original OP.) However, it appears to be:

void this_function(..., int *EmployeeSize, ...)
{
    ...code...
}
┈┾☆殇 2024-10-29 10:45:34

表达式*EmployeeSize++ 返回EmployeeSize 指向的值,然后递增指针,而不是指向的项。尝试(*EmployeeSize)++

The expression *EmployeeSize++ returns the value pointed to by EmployeeSize and then increments the pointer, not the pointed-to item. Try (*EmployeeSize)++.

梦初启 2024-10-29 10:45:34
GivenEmployees[(*EmployeeSize++)]

闻起来像麻烦。它被解析为

GivenEmployees[(*(EmployeeSize++))]

后缀增量比解引用具有更高的优先级。
因此,您增加一个指针,然后取消引用它。 “EmployeeSize”是指向数组的指针吗?

GivenEmployees[(*EmployeeSize++)]

Smells like trouble. It is parsed as

GivenEmployees[(*(EmployeeSize++))]

Postfix incrementation has higher precedence than dereferencing.
So, you increment a pointer and then dereference it. Is 'EmployeeSize' a pointer to an array?

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