3 ms·
We're not talking about C, were talking about how it works on the computers.
by enedil 6y ago
We're not talking about C, were talking about how it works on the computers.
- labawi 6y agoMany if not most of the embedded/long term systems are implemented in C. If a variable is declared signed, overflowing cases may and often are "optimized" away. IIUC, GP's foo() would likely be optimized to { return true; }, and so would similar timestamp overflow checks.
- deleted 6y ago[deleted]
- dragontamer 6y agoThe post I responded to was the opposite: about turning "signed" code (which you declare is undefined) into "unsigned" code (which you declare is fully defined). Given this thread of subargument, making the difference between 32-bit unsigned numbers is MORE DEFINED than using signed integers. ------- IE: If your code was correct with "int timestamp", it will be more correct with "unsigned int timestamp". In any case, "int" or "unsigned int" based timestamp manipulation wouldn't be like the code you suggested, but instead "int difference = x - y". In the signed integer case, "difference" is (conceptually) negative, while in the unsigned integer case, "difference" is guaranteed to have overflow. Both cases are conceptually correct with regards to the difference of timestamps.
- shakna 6y agoBut because the behaviour is undefined, it doesn't matter how the computer would handle it, because the compiler is free to rework it into any arbitrary sequence of instructions, including removing it altogether.
- ncmncm 6y agoThere are exactly zero undefined behaviors around operations on unsigned integer types in C or in C++. To get meaningful results may require some care, but the languages provide everything needed to exercise such care.
- shakna 6y agoPerhaps you missed my earlier comment. > Except signed overflow invoking Undefined Behaviour in any C compiler, whereas unsigned overflow does not. We aren't talking about unsigned integer types. We're talking about the behaviour of signed integer types.