Note that there are some explanatory texts on larger screens.

plurals
  1. PONaN handled differently in different g++ versions
    primarykey
    data
    text
    <p>Consider the following program, which is obviously buggy:</p> <pre><code>#include &lt;cstdio&gt; double test(int n) { if (n % 2 == 0) return 0.0; // warning: control reaches end of non-void function } int main() { printf("%.9lf\n", test(0)); printf("%.9lf\n", test(1)); printf("%.9lf\n", test(2)); printf("%.9lf\n", test(3)); return 0; } </code></pre> <p>When compiled with g++, version 4.2.4 (Ubuntu 4.2.4-1ubuntu4) on a 32-bit Ubuntu 8.04, it produces the following output:</p> <pre><code>0.000000000 nan 0.000000000 nan </code></pre> <p>When compiled with g++, version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) on a 64-bit Ubuntu 10.04, it produces the following output:</p> <pre><code>0.000000000 0.000000000 0.000000000 0.000000000 </code></pre> <p>It seems that the older compiler does some extra work in order to return NaN instead of garbage, while the newer compiler simply returns whatever there is in memory. What exactly causes this difference in behavior and how to control it and make it predictable across different compiler versions?</p> <p>EDIT: Sorry for not mentioning undefined behavior earlier. I know that the difference comes from the fact that this program has undefined behavior. What I'd like to know <strong>why</strong> the former gcc version seems to put some effort and produce code that consistently returns NaN and <strong>when</strong> this behavior changed to the one observed in the latter gcc version. Also, by "predictable" I meant not how to write good programs in C++, but how to control this gcc behavior (maybe with some command line options?).</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.
 

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