2

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 also throws an error.

error: integer overflow in expression [-Werror=overflow]
         uint64_t TESTBYTES = 16 * 1024 * 1024 * 1024;

2 Answers 2

13

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 since an unsigned long long needs to be at least 64 bits.

Sign up to request clarification or add additional context in comments.

4 Comments

With what data type?
what is ULL? The other question being: If at somepoint I define another variable called uint64_t TEST1BYTES and adding 64bytes to it.. like TEST1BYTES+=64; It will throw an error once it reaches the limit. BTW, Thanks
A uint64_t must wrap around to zero if you exceed the highest value, so no error would be thrown. But you'd do well to overflow a 64 bit unsigned by successive additions of 64; would take a few years even with an increment per nanosecond.
True that. Just asking - that's might happen.
1

As-is your expression consists of integer literals without suffixes and will be evaluated as a signed integer and the value will overflow. The result of your expression is 17179869184 and the max value the 32-bit integer can hold is 2147483647 so it will overflow. To force the expression to be evaluated as unsigned (unsigned long long in your case) value you should add the ULL or ull suffix to one of your operands (except the final one as pointed out in the comments) so that the entire expression is evaluated as unsigned long long. Trivial example to demonstrate what happens:

#include <iostream>
#include <cstdint>
#include <limits>

int main() {
    uint64_t x; // unsigned long long
    auto y = 16 * 1024 * 1024 * 1024; // evaluated as int, the result overflows
    auto z = 16ull * 1024 * 1024 * 1024; // evaluated as unsigned long long, does not overflow
    std::cout << "Your expression: " << z << '\n';
    std::cout << "Maximum integer value: " << std::numeric_limits<int>::max() << '\n';
    std::cout << "Maximum unsigned long long value: " << std::numeric_limits<unsigned long long>::max() << '\n';
    x = z;
}

5 Comments

Not any one argument: adding it, say, to the final one makes the code vulnerable on platforms with 16 bit int.
this would still fail in both cases.. uint64_t CapacityBytes[] = {16ULL * 1024 * 1024 * 1024, 16ULL * 1024 * 1024 * 1024};
Thanks for the comment. Could you please also address the above question?
The overflow is in the multiplication expression, not the deduction of variable type. It is not correct to say "the result doesn't fit" for the y line
@M.M: I think as an array the problem is with error: too many initializers for ‘uint64_t [0] not that we are having multiplication expressions.. because unint64_t[2].. works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.