0

Is this undefined behavior?

unsigned int size = 0;
size -= 1;
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 3
    No. The C standard guarantees modulo-power-of-two wraparound. 6.2.5 _Types_ clause 9 says: _A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type._ – Iwillnotexist Idonotexist Jan 11 '15 at 00:11
  • It's an interesting question in the context of a recent question asking if `int i = INT_MAX + 1;` causes UB. But following @5gon12eder I like https://stackoverflow.com/questions/18195715/why-is-unsigned-integer-overflow-defined-behavior-but-signed-integer-overflow-is – Weather Vane Jan 11 '15 at 00:24
  • @WeatherVane: I think an implementation could legitimately define its numeric types such that incrementing an unsigned value of a type which ranks below `int` could yield Undefined Behavior, but I don't think that's true of decrementing. If an unsigned value smaller than `int` gets promoted to `int`, that `int` will be able to represent both 0 and -1 without difficulty; coercing the signed value -1 to an unsigned type of any size is required to yield the value which, when converted to a suitably-large type, incremented, and cast back, will yield zero. – supercat Jun 24 '15 at 15:25

1 Answers1

9

Unsigned integers have well-defined behaviour for all arithmetic operations; specifically, they implement "artithmetic modulo 2N", where N is the number of value bits in the type. So -1 is in fact 2N − 1.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 2
    does that mean that `unsigned int x; for (x = SOME_POSITIVE_VALUE ; x >= 0 ; --x);` is an infinite loop? – Iharob Al Asimi Jan 11 '15 at 00:13
  • 2
    (More interestingly than addition are the shifting and remainder operations, which are also much simpler to reason about with unsigned than with signed integers.) – Kerrek SB Jan 11 '15 at 00:13
  • 3
    @iharob Yes. By definition, `unsigned` integers are `>= 0`. – Iwillnotexist Idonotexist Jan 11 '15 at 00:14
  • 1
    @iharob: For an unsigned integer `x`, `x >= 0` is always true, yes. – Kerrek SB Jan 11 '15 at 00:14
  • I was just making sure that it was not `undefined behavior`, I don't own any copy of the c standard and since I live in Venezuela can't afford one, particulary nowadays. – Iharob Al Asimi Jan 11 '15 at 00:18
  • All arithmetic operations except division and modulo by zero and bitshifts bigger than the width of the data type and quite possibly something else. Unsigned int division also isn't the same as "division" modulo N. (OK, I'm done being persnickety.) – tmyklebu Jan 11 '15 at 00:19
  • @iharob Hardly anyone has _the_ Standard, everybody really quotes the draft. [Here's the newest draft standard available, n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), which is the final draft before the C11 standard's release. – Iwillnotexist Idonotexist Jan 11 '15 at 00:23
  • @IwillnotexistIdonotexist thank you for the link, already downloading it. – Iharob Al Asimi Jan 11 '15 at 00:24