开发者

How to get rid of integer overflow warning?

开发者 https://www.devze.com 2023-03-31 18:35 出处:网络
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\'.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消