title: Should Local Variable Initialisation Be Mandatory?
body: <p>The maintenance problems that uninitialised locals cause (particularly pointers) will be obvious to anyone who has done a bit of c/c++ maintenance or enhancement, but I still see them and occasionally hear performance implications given as their justification.</p>
  
  <p>It's easy to demonstrate in c that redundant initialisation is optimised out:</p>
  
  <pre><code>$ less test.c
  #include &lt;stdio.h&gt;
  main()
  {
  #ifdef INIT_LOC
      int a = 33;
      int b;
      memset(&amp;b,66,sizeof(b));
  #else
      int a;
      int b;
  #endif
      a = 0;
      b = 0;
      printf ("a = %i, b = %i\n", a, b);
  }
  
  $ gcc --version
  gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)
  </code></pre>
  
  <p>[Not Optimised:]</p>
  
  <pre><code>$ gcc test.c -S -o no_init.s; gcc test.c -S -D INIT_LOC=1 -o init.s; diff no_in
  it.s init.s
  22a23,28
  &gt;       movl    $33, -4(%ebp)
  &gt;       movl    $4, 8(%esp)
  &gt;       movl    $66, 4(%esp)
  &gt;       leal    -8(%ebp), %eax
  &gt;       movl    %eax, (%esp)
  &gt;       call    _memset
  33a40
  &gt;       .def    _memset;        .scl    3;      .type   32;     .endef
  </code></pre>
  
  <p>[Optimised:]</p>
  
  <pre><code>$ gcc test.c -O -S -o no_init.s; gcc test.c -O -S -D INIT_LOC=1 -o init.s; diff
   no_init.s init.s
  $
  </code></pre>
  
  <p>So WRT performance under what circumstances is mandatory variable initialisation NOT a good idea?</p>
  
  <p>IF applicable, no need to restrict answers to c/c++ but please be clear about the language/environment (and reproducible evidence much preferred over speculation!)</p>