问题描述
我试图以字节为单位表示16gb,而 uint64_t
抛出错误.
I'm trying to represent 16gb in bytes, and uint64_t
throws an error.
我应该使用哪种数据类型来表示它? unsigned long int
也会引发错误.
What kind of data type should I use to represent it?unsigned long int
also throws an error.
error: integer overflow in expression [-Werror=overflow]
uint64_t TESTBYTES = 16 * 1024 * 1024 * 1024;
推荐答案
uint64_t TESTBYTES = 16ULL * 1024 * 1024 * 1024
可以做到.
否则,表达式 16 * 1024 * 1024 * 1024
被计算为 int
,由于溢出 int ,在您的平台上结果未定义代码>类型.
Else the expression 16 * 1024 * 1024 * 1024
is evaluated as an int
, with undefined results on your platform since you are overflowing the int
type.
ULL
将第一个术语提升为 unsigned long long
,从而强制提升其他术语.这是始终的安全保证, unsigned long long
至少需要 64位.
ULL
promotes the first term to an unsigned long long
, forcing promotion of the other terms. This is always safe singe an unsigned long long
needs to be at least 64 bits.
这篇关于CPP错误:表达式中的整数溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!