CPP Error: Integer overflow in expression

前端 未结 2 818
甜味超标
甜味超标 2020-12-12 03:21

I\'m trying to represent 16gb in bytes, and uint64_t throws an error.

What kind of data type should I use to represent it? unsigned long int

2条回答
  •  萌比男神i
    2020-12-12 03:47

    uint64_t TESTBYTES = 16ULL * 1024 * 1024 * 1024 will do it.

    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 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.

提交回复
热议问题