The code is in a .cu file, and compiled using nvcc. I get the compilation warning that for the line “size = 5000 * 1024 * 1024”, the “integer operation result is out of range”. The output I got is
sizeof(size) = 8 size = 947912704
I don’t understand why the variable “size” can’t represent the value 5242880000 if it’s 8-bytes large.
In the expression “5000 * 1024 * 1024” all operands are assumed to be of type “int”, by normal C/C++ rules. Thus the whole expression is evaluated as “int”, before the result is assigned to a variable of type “size_t”. Since an “int” is too small to hold the product of 5000 * 1024 * 1024 the compiler emits a warning.
You can change the type of the integer constants with an appropriate suffix, such as LL for “long long”. For example, the following should get rid of the warning, since declaring the first literal integer constant to be “long long” forces evaluation of the entire expression as “long long”, and a “long long” can easily hold the product of the three factors: