title: C++ : how to make sure all variables are initialized?
body: <p>Recently I had lots of trouble with a non initialized variable. </p>
  
  <p>In Java, the default value of variable is null, therefore an exception is likely to be thrown when if the non-initialized variable is used. If I understood, in C++, the variable is initialized with whatever data turns out to be in the memory. Which means that the program is likely to run, and it might be hard to even know there is something wrong with it.</p>
  
  <p>What would be the clean way to deal with this ? Is there some good programming habit that would reduce the risk ? In my case, the variable was declared in the header file and should have been initialized in the cpp file, which is an example of things that makes error more likely. </p>
  
  <p>thx</p>
  
  <hr>
  
  <p>Edition after receiving few answers:</p>
  
  <p>My apologies, my question was not specific enough.</p>
  
  <p>The answer I get to use flag for the compilers to get informed of non-initialized variables will be useful.</p>
  
  <p>But there are rare cased variables can not be initialized at the beginning, because depending on the behavior of your system. </p>
  
  <p>in header file</p>
  
  <pre><code>double learnedValue;
  </code></pre>
  
  <p>in cpp file</p>
  
  <pre><code>/* code that has nothing to do with learnedValue
  ...
  */
  
  learnedValue = a*b*c; // values of a, b and c computed in the code above
  
  /*code making use of learned value
  ...
  */
  </code></pre>
  
  <p>Now what happened is that forgot the line "learnedValue=a*b*c".</p>
  
  <p>But the program was working good, just with value of learnedValue initialized with whatever what was in the memory when it was declared.</p>
  
  <p>In Java, such error is not an issue, because the code making use of learned value is likely to crash or throw an exception (at least you get to know what was wrong).</p>
  
  <p>In C++, you can apparently be happy and never get to know there is a problem at all. Or ?</p>