Note that there are some explanatory texts on larger screens.

plurals
  1. POTentative definitions in C99 and linking
    text
    copied!<p>Consider the C program composed of two files,</p> <p>f1.c:</p> <pre><code>int x; </code></pre> <p>f2.c:</p> <pre><code>int x=2; </code></pre> <p>My reading of paragraph 6.9.2 of <a href="http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1124.pdf" rel="noreferrer">the C99 standard</a> is that this program should be rejected. In my interpretation of 6.9.2, variable <code>x</code> is tentatively defined in <code>f1.c</code>, but this tentative definition becomes an actual definition at the end of the translation unit, and (in my opinion), should therefore behave as if <code>f1.c</code> contained the definition <code>int x=0;</code>.</p> <p>With all compilers (and, importantly, linkers) I was able to try, this is not what happens. All compilation platforms I tried do link the above two files, and the value of <code>x</code> is 2 in both files.</p> <p>I doubt this happens by accident, or just as an "easy" feature to provide in addition to what the standard requires. If you think about it, it means there is special support in the linker for those global variables that do not have an initializer, as opposed to those explicitly initialized to zero. Someone told me that the linker feature may be necessary to compile Fortran anyway. That would be a reasonable explanation.</p> <p>Any thoughts about this? Other interpretations of the standard? Names of platforms on which files <code>f1.c</code> and <code>f2.c</code> refuse to be linked together?</p> <p>Note: this is important because the question occurs in the context of static analysis. If the two files may refuse to be linked on some platform, the analyzer should complain, but if every compilation platform accepts it then there is no reason to warn about it.</p>
 

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