gcc7.2:参数范围超出了最大对象大小9..7 [-Werror = alloc-size-larger-than =] [英] gcc7.2: argument range exceeds maximum object size 9..7 [-Werror=alloc-size-larger-than=]

查看:341
本文介绍了gcc7.2:参数范围超出了最大对象大小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));

错误消失了.

但是,在程序中,有很多malloccalloc用作我的原始版本.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆