Integer operation result out of range

Hi all,

I’m trying to compile a code written using CUDA 3.2 on RHEL 5.6. The relevant portions are

extern "C"{

    #include <stdio.h>

    #include <inttypes.h>

    static uint64_t size = 0;

    ...

    size = 5000 * 1024 * 1024;

    printf("sizeof(size) = %d size = %lu\n", sizeof(size), size);

}

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.

Thank you.

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:

size = 5000LL * 1024 * 1024;

Thanks! This worked!