开发者

Are intentional value overflows safe?

This question is probably stupid or paranoidal, but anyway :-). Given following code:

DWORD hiRes;

// will overflow about once in an hour
hiRes = GetTickCount() * 1193L;

If it known that hiRes overflows periodically and such situations are handled properly, is there anything wrong with this code?

UPDATE: Result is quite su开发者_开发技巧rprising for me, since the answer depends on the type of hiRes (signed or unsigned), which is defined by C Standard (see for example).


Overflowing an unsigned int is safe. Overflowing a signed one isn't (undefined behavior).

MSDN says:

A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal). This type is declared as follows:

typedef unsigned long DWORD

So it should be safe.


Unsigned integers are safe, signed are not. But I've never come accross a platform that doesn't do the obvious twos complement thing. I do wish the standards people had bitten the bullet and just made it mandatory.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜