Search code examples
cwindowsinteger-overflow

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 surprising for me, since the answer depends on the type of hiRes (signed or unsigned), which is defined by C Standard (see for example).


Solution

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