Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first thing you have to know is, in C, <strong>integer overflows</strong> are undefined behavior.</p> <blockquote> <p>(C99, 6.5.5p5) "If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined."</p> </blockquote> <p>C says it very clear and repeats it here:</p> <blockquote> <p>(C99, 3.4.3p3) "EXAMPLE An example of undefined behavior is the behavior on integer overflow."</p> </blockquote> <p>Note that integer overflows only regard signed integer as unsigned integer never overflow:</p> <blockquote> <p>(C99, 6.2.5p9) "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."</p> </blockquote> <p>Your declaration is this one:</p> <pre><code>int i = 400 * 400 / 400; </code></pre> <p>Assuming <code>int</code> is 16-bit in your platform and the signed representation is two's complement, <code>400 * 400</code> is equal to <code>160000</code> which is not representable as an <code>int</code>, <code>INT_MAX</code> value being <code>32767</code>. We are in presence of an integer overflow and the implementation can do <strong>whatever</strong> it wants. </p> <p>Usually, in this specific example, the compiler will do one of the two solutions below:</p> <ol> <li>Consider the overflow wraps around modulo the word size like with unsigned integers then the result of the <code>400 * 400 / 400</code> is <code>72</code>.</li> <li>Take advantage of the undefined behavior to reduce the expression <code>400 * 400 / 400</code> to <code>400</code>. This is done by good compilers usually when the optimization options are enabled.</li> </ol> <p>Note that that integer overflows are undefined behavior is specific to C language. In most languages (Java for instance) they wrap around modulo the word size like unsigned integers in C.</p> <p>In <code>gcc</code> to have overflow always wrap, there is the <code>-fno-strict-overflow</code> option that can be enabled (disabled by default). This is for example the choice of the Linux kernel; they compile with this option to avoid bad surprises. But this also constraints the compiler to not perform all optimizations it can do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload