gcc7.2:参数范围超出了最大对象大小9..7 [-Werror = alloc-size-larger-than =] [英] gcc7.2: argument range exceeds maximum object size 9..7 [-Werror=alloc-size-larger-than=]
问题描述
程序包含如下代码:
int size;
...
int *pi = (int*)calloc(size, sizeof(int));
...
以下是使用gcc7.2编译时的错误消息:
Here is the error message when compiled with gcc7.2:
错误:参数1范围[18446744071562067968,18446744073709551615]超出了最大对象大小9223372036854775807 [-Werror = alloc-size-larger-than =]
error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
当我改变
int *pi = (int*)calloc(size, sizeof(int));
至
int *pi = (int*)calloc((unsigned int)size, sizeof(int));
When I change
int *pi = (int*)calloc(size, sizeof(int));
to
int *pi = (int*)calloc((unsigned int)size, sizeof(int));
错误消失了.
但是,在程序中,有很多malloc
和calloc
用作我的原始版本.
However, in the program, there are many malloc
and calloc
used like my original version.
为什么gcc仅检测到一个错误?
Why there is only one error detected by gcc?
推荐答案
我最近在GCC 9.1的构建中遇到了同样的问题,而我在GCC Bugzilla上发现了以下讨论:
I recently had the same problem on my GCC 9.1 build, and I found this discussion on the GCC Bugzilla:
https://gcc.gnu.org/bugzilla//show_bug .cgi?id = 85783
如链接讨论中所述,我能够通过根据PTRDIFF_MAX检查size参数来抑制警告.
As mentioned in the link discussion, I was able to suppress the warning by checking the size parameter against PTRDIFF_MAX.
这篇关于gcc7.2:参数范围超出了最大对象大小9..7 [-Werror = alloc-size-larger-than =]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!