gpt4 book ai didi

c - 即使使用 unsigned long int 也会出现溢出

转载 作者:太空狗 更新时间:2023-10-29 16:22:55 25 4
gpt4 key购买 nike

当我进行以下计算时:

unsigned long long int data_size = 60123456 * 128 * sizeof(double);
printf("data_size= %llu \n", data_size);

我出乎意料地收到了溢出警告:

test.c:20:49: warning: overflow in expression; result is -894132224 with type 'int' [-Winteger-overflow]
unsigned long long int data_size = 60123456 * 128 * sizeof(double);
^
1 warning generated.

即使我正在使用 unsigned long long int,我也无法理解为什么会出现此错误!有人可以解释为什么吗?谢谢

最佳答案

溢出发生在 值分配给变量之前。为避免使用 long long 后缀:

60123456LL * 128 * sizeof(double)

(评论还建议使用 ULL,这在这里不是必需的,因为值在有符号范围内,但这是通用的方法)

关于c - 即使使用 unsigned long int 也会出现溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45281718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com