Question

Having read all answers and comments in Should you always use 'int' for numbers in C, even if they are non-negative? I'm still not sure what to do in the following situation.

Two remote devices are counting events at their respective location. They regularly report the counter readings to a PC where a feedback algorithm somehow keeps the difference of the counter readings within bounds. The sequences of events are endless so that the readings are reported modulo 2^n. For modulo arithmetic unsigned integers are suggested. The difference, however, may well become negative. Casting the readings to signed integers before calculating the difference works fine on the platform I tested it on (that is, I get the differences modulo 2^n with values small in magnitude). Declaring the readings as signed already in the interface yields elegant code. However, the code shall be portable. Shall I take the U.B. warning seriously?

Was it helpful?

Solution

Shall I take the U.B. warning seriously?

If you don't want your compiler or CPU play tricks on you, your code should be UB-free.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top