سؤال

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 as '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).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top