意外的整数换行
我遇到一个整数意外回绕到最小值的问题。
在换行到 -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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是你正在增加指针 - 它最终指向随机区域。
您可能打算写:
这里括号是必需的;它们在原作中是不必要的。
来自评论:
(除了它在原始OP中被称为“大小”。)但是,它似乎是:
The problem is that you are incrementing the pointer - and it ends up pointing into random territory.
You probably intended to write:
The parentheses are necessary here; they are unnecessary in the original.
From the comments:
(Except that it is called 'Size' in the original OP.) However, it appears to be:
表达式
*EmployeeSize++
返回EmployeeSize
指向的值,然后递增指针,而不是指向的项。尝试(*EmployeeSize)++
。The expression
*EmployeeSize++
returns the value pointed to byEmployeeSize
and then increments the pointer, not the pointed-to item. Try(*EmployeeSize)++
.闻起来像麻烦。它被解析为
后缀增量比解引用具有更高的优先级。
因此,您增加一个指针,然后取消引用它。 “EmployeeSize”是指向数组的指针吗?
Smells like trouble. It is parsed as
Postfix incrementation has higher precedence than dereferencing.
So, you increment a pointer and then dereference it. Is 'EmployeeSize' a pointer to an array?