문제

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