In my C++ code I have an expression where I multiply unsigned long integer to an int to assign the result to unsigned long int. I am getting a warning a开发者_如何学Gos 'overflowed returned value'. I tried to cast the int to unsigned long but it did not help. Any suggestions...
The expression is something like this-
uint64_t size = 0;
uint64_t value = getvalue();
int pageSize= getPageSize();
size = value*(uint64_t)pageSize;
The following should do the job:
size = value*(uint64_t)(unsigned int)pageSize;
Your tool probably has a way to set an exception for this line of code.
If the tool is truly clever, this is the only way, for there really is a risk of overflow here (as an int
may not accomodate 2^64-1
).
精彩评论