Note that there are some explanatory texts on larger screens.

plurals
  1. POInitialise a variable to its own undefined value
    primarykey
    data
    text
    <p>In C, does initialising a variable to it's own value make sense? If yes, what for?</p> <p>Allow me to elaborate. In Git sources there are some examples of initialising a variable to it's own undefined value, as seen in <a href="https://github.com/git/git/blob/f84667def209e4a84e37e8488a08e9eca3f208c1/transport.c#L109" rel="nofollow">transport.c</a> or <a href="https://github.com/git/git/blob/f84667def209e4a84e37e8488a08e9eca3f208c1/wt-status.c#L267" rel="nofollow">wt-status.c</a>. I removed assignments from those declarations and run tests. Seeing no regressions, I thought that those assignments were redundant.</p> <p>On the other hand, I did some simple tests with GCC 4.6 and Clang 2.9.</p> <pre><code>#include &lt;stdio.h&gt; int main() { printf("print to increase probability of registers being non-zero\n"); int status = status; return printf("%i\n", status); } </code></pre> <p>Compiling with <code>-Wall -std=c99</code> and various <code>-O</code> levels prints no warnings and shows that <code>status == 0</code>. Clang with a non-zero optimisation level prints some garbage values though. It makes me infer that results of such expressions are undefined.</p> <p>I can imagine that such assignment can suppress an <em>uninitialised variable</em> warning, but it's not the case for the examples taken from Git. Removing assignments doesn't introduce any warnings.</p> <p>Are such assignments an undefined behaviour? If not, what do you use them for?</p> <hr> <p>I've suggested the change on the Git mailing list. <a href="http://article.gmane.org/gmane.comp.version-control.git/185969" rel="nofollow">Here's what I've learned</a>.</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