Re: Detecting overflows while computing off_t



In article <slrneoncai.2rv.cheney@xxxxxxxxxxxxxxxx>,
Andre Majorel <cheney@xxxxxxxxxxxxxxx> wrote:

How do you compute an off_t with overflow detection ?

A sometimes useful fact, if you know that overflow behaves as addition
mod 2^N (where N is the size in bits), is that a+b overflows if and
only if a+b < a (for positive a and b). So you can do the addition
and check for overflow by comparing the result to either of the
operands. For unsigned integer types in C, overflow must behave this
way. For signed types, it is still true for most implementations.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
.



Relevant Pages

  • Re: Detecting overflows while computing off_t
    ... if you know that overflow behaves as addition ... For unsigned integer types in C, ... For signed types, it is still true for most implementations. ...
    (comp.lang.c)
  • Re: Detecting overflows while computing off_t
    ... A sometimes useful fact, if you know that overflow behaves as addition ... For signed types, it is still true for most implementations. ...
    (comp.lang.c)
  • Re: compare a large number of variables
    ... >>> The subject in question is overflow, ... >>> usually is understood to refer to signed integer types rather ... it also states that unsigned integer types DO behave a certain way. ... >>> the ceiling function. ...
    (comp.lang.c)
  • Re: compare a large number of variables
    ... it also states that unsigned integer types DO behave a certain way. ... > unsigned operands can never overflow," 6.2.5 p9. ... > with overflow (or underflow) for signed integer types. ...
    (comp.lang.c)
  • Re: overflow of unsigned int
    ... Unsigned integer types cannot overflow. ... confusion: ... "wrap around" precisely, ...
    (comp.lang.c)