Minoto wrote:
What happened when you wrote the code and ran it?
Signed integer overflow normally has undefined behaviour in C. The only documentation I've seen for InterlockedAdd doesn't say that there's any exception for it in regards to this, so I would strongly suggest not using any observed behaviour as a reliable indicator of general behaviour.
Edit: in general an unsigned integer value can be safely converted to a signed integer value of the same size (the result is implementation-defined in C99; all common compilers for common architectures that I know of will just reinterpret the bitwise value as a signed value, effectively wrapping since the representation is 2's complement). So, for this particular example it should be perfectly fine to use unsigned values to effectively subtract some amount from some integer, so long as the result when interpreted as an operation on signed integers doesn't overflow. Specifically:
Quote:
Probably 8 + (-1) which is 8 + 0xFFFFFFFFFFFFFFFF will lead to 7 ?? or an overflow ?
The -1 will be treated as a signed value because _InterlockedAdd takes signed arguments, i.e. -1u becomes -1 (again: subject to implementation-defined behaviour). Hence there is no overflow for this example (assuming the usual implementation-defined behaviour).
However, my point stands: if you believe that an operation may involve overflow, do not attempt to determine the behaviour by observing it. If the behaviour is undefined, it will not necessarily behave consistently.