Integer overflow with structure overflow

For compilers that do not support 64-bit arithmetic, there is a ULARGE_INTEGER union .

What happens in the following code if the addition in the last line overflows?

ULARGE_INTEGER u; u.LowPart = ft->dwLowDateTime; u.HighPart = ft->dwHighDateTime; u.LowPart += 10000; //what if overflow? 

Related question: What is the meaning of the ULARGE_INTEGER union?

+2
source share
1 answer

ULARGE_INTEGER consists of two unsigned values. Unsigned values ​​are guaranteed to be rounded, so in a sense they cannot be "overflowed."

If the wrap is complete, u.LowPart will be less than 10,000. You probably want to:

 u.LowPart += 10000; if (u.LowPart < 10000) u.HighPart++; 

... but which compiler still does not support 64-bit integers? They have been required by the C ++ standard since 2011 and the C standard since 1999. So what you really want:

 u.QuadPart += 10000; // Forget about legacy compilers that doen't support 64 bits. 
+7
source

All Articles