有符号整数溢出
Signed integer overflow
在下面的源代码中(我已经在代码上注释了错误),我得到
Line 27: Char 33: runtime error: signed integer overflow: -1094795586+ -1094795586 cannot be represented in type 'int'
我查阅了不同的文章,例如 How to detected signed integer overflow,但我不知道要检查哪个整数。
如果 working_array[]
的元素的值小于 target
整数,则从 nums[]
中过滤掉它们。
由于 nums[]
的元素类似于 [ 3, 6, 11, 19, 2, ...]
我不担心在最后一个 if
语句的 SUM 期间检查溢出。我错了?
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int count_elem = 0;
int *working_array = (int*)malloc(numsSize * sizeof(int));
// Going through original array
for(int i=0;i<numsSize;i++){
// If elem nums[i] is less than target, is accepted on a new working array
if(nums[i] < target){
count_elem += 1;
working_array[count_elem] = nums[i];
}
}
working_array = realloc(working_array, count_elem*sizeof(int));
// Creating result array
returnSize = sizeof(int)*2;
int *result_array = (int*)malloc(returnSize);
// Going through working array
for(int i=0;i<count_elem;++i)
for(int j=0;j<count_elem;++j)
// SIGNED INTEGER OVERFLOW IN LINE BELOW
if( (working_array[i]+working_array[j]) == target){
result_array[0] = working_array[i];
result_array[1] = working_array[j];
free(working_array);
return result_array;
}
free(working_array);
return;
}
P.S:我知道cast malloc results没用,但这可能是个小问题。
if(nums[i] < target){
count_elem += 1;
working_array[count_elem] = nums[i];
}
差一错误。这会初始化 working_array[1]
、working_array[2]
、... 可能高达 working_array[numsSize]
,这将超出范围。同时,working_array[0]
从未被初始化并且包含垃圾,这可能是引起溢出的垃圾值。
成功:
if(nums[i] < target){
working_array[count_elem] = nums[i];
count_elem += 1;
}
或者也许
if(nums[i] < target){
working_array[count_elem++] = nums[i];
}
此外,如上所述,
returnSize = sizeof(int)*2;
int *result_array = (int*)malloc(returnSize);
是错误的,因为 returnSize
是一个指针。如果想法是通过引用 return 大小,那么你想要
*returnSize = sizeof(int)*2;
int *result_array = (int*)malloc(*returnSize);
在下面的源代码中(我已经在代码上注释了错误),我得到
Line 27: Char 33: runtime error: signed integer overflow: -1094795586+ -1094795586 cannot be represented in type 'int'
我查阅了不同的文章,例如 How to detected signed integer overflow,但我不知道要检查哪个整数。
如果 working_array[]
的元素的值小于 target
整数,则从 nums[]
中过滤掉它们。
由于 nums[]
的元素类似于 [ 3, 6, 11, 19, 2, ...]
我不担心在最后一个 if
语句的 SUM 期间检查溢出。我错了?
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int count_elem = 0;
int *working_array = (int*)malloc(numsSize * sizeof(int));
// Going through original array
for(int i=0;i<numsSize;i++){
// If elem nums[i] is less than target, is accepted on a new working array
if(nums[i] < target){
count_elem += 1;
working_array[count_elem] = nums[i];
}
}
working_array = realloc(working_array, count_elem*sizeof(int));
// Creating result array
returnSize = sizeof(int)*2;
int *result_array = (int*)malloc(returnSize);
// Going through working array
for(int i=0;i<count_elem;++i)
for(int j=0;j<count_elem;++j)
// SIGNED INTEGER OVERFLOW IN LINE BELOW
if( (working_array[i]+working_array[j]) == target){
result_array[0] = working_array[i];
result_array[1] = working_array[j];
free(working_array);
return result_array;
}
free(working_array);
return;
}
P.S:我知道cast malloc results没用,但这可能是个小问题。
if(nums[i] < target){
count_elem += 1;
working_array[count_elem] = nums[i];
}
差一错误。这会初始化 working_array[1]
、working_array[2]
、... 可能高达 working_array[numsSize]
,这将超出范围。同时,working_array[0]
从未被初始化并且包含垃圾,这可能是引起溢出的垃圾值。
成功:
if(nums[i] < target){
working_array[count_elem] = nums[i];
count_elem += 1;
}
或者也许
if(nums[i] < target){
working_array[count_elem++] = nums[i];
}
此外,如上所述,
returnSize = sizeof(int)*2;
int *result_array = (int*)malloc(returnSize);
是错误的,因为 returnSize
是一个指针。如果想法是通过引用 return 大小,那么你想要
*returnSize = sizeof(int)*2;
int *result_array = (int*)malloc(*returnSize);