[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Please don't do this (code fragment)



>>"Olaf" == Olaf Weber <olaf@infovore.xs4all.nl> writes:

 Olaf> Thomas Bushnell writes:
 >> Actually, the C standard does essentially guarantee two's complement
 >> arithmetic.  It specifies integer overflow behavior and
 >> signed/unsigned conversion behavior exactly.

 Olaf> It does for unsigned integers, but for signed integers overflow is
 Olaf> undefined behaviour. 

	Umm. Please note from
======================================================================
  3.1.2.4 Types

   ... 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 unsigned integer type.
======================================================================

	Also:
======================================================================
 3.2.1.2 Signed and Unsigned Integers
 
 When a value with integral type is converted to another integral
 type, if the value can be represented by the new type, its value is
 unchanged. 

 When a signed integer is converted to an unsigned integer with equal
 or greater size, if the value of the signed integer us nonnegative
 its value is unchanged,  Otherwise if the unsigned integer has
 greater size the signed integer is first promoted to the signed
 integer corresponding to the unsigned integer; the value is
 converted to unsigned by adding to it one greater than the largest
 number that can be represented in the unsigned integer type. 

	When a value with integral type is demoted to an unsigned
 integer with smaller size the result is the nonnegative remainder on
 division by the number one greater than the largest unsigned number
 that can be represented in the type with smaller size.. When a value
 with integral type is demoted to a signed integer with smaller size,
 or an unsigned integer us converted to its corresponding sign4ed
 integer, if the value cannot be represented the result is
 implementation-defined.
======================================================================


	Clear? ;-)

 Olaf>  The clearest statement of that is 3.4.3, albeit
 Olaf> in an example:

 Olaf>    3.4.3
 Olaf>  1 undefined behavior behavior, upon use of a nonportable or erroneous
 Olaf>    program construct or of erroneous data, for which this International
 Olaf>    Standard imposes no requirements
 Olaf>  2 NOTE Possible undefined behavior ranges from ignoring the situation
 Olaf>    completely with unpredictable results, to behaving during translation
 Olaf>    or program execution in a documented manner characteristic of the
 Olaf>    environment (with or without the issuance of a diagnostic message),
 Olaf>    to terminating a translation or execution (with the issuance of a
 Olaf>    diagnostic message). 
 Olaf>  3 EXAMPLE An example of undefined behavior is the behavior on integer
 Olaf>    overflow.

	manoj
-- 
 Somebody's terminal is dropping bits.  I found a pile of them over in
 the corner.
Manoj Srivastava   <srivasta@debian.org>  <http://www.debian.org/%7Esrivasta/>
1024R/C7261095 print CB D9 F4 12 68 07 E4 05  CC 2D 27 12 1D F5 E8 6E
1024D/BF24424C print 4966 F272 D093 B493 410B  924B 21BA DABB BF24 424C



Reply to: