title: C++ memory: deleting an unused array of bool can change the result from right to wrong
body: <p>I am trying to solve Project Euler <a href="https://projecteuler.net/problem=88" rel="nofollow">Problem 88</a>, and I did it without too much effort; however, I find that some seemingly irrelevant code in my program is affecting the result. Here's my complete code (it's not short, but I cannot locate the error. I believe it would be obvious to more experienced eyes, so please read my description first):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;set&gt;
  
  using namespace std;
  
  bool m[24001][12001];
  bool p[24001]; // &lt;------------ deleting this line will cause error in result!
  long long answer[12001];
  
  int main() {
    long long i;
    long long j;
    long long l;
    set&lt;long long&gt; all;
    long long s = 0;
    for (i = 0; i &lt;= 24000; i++) {
          for (j = 0; j &lt;= 12000; j++) {
        m[i][j] = false;
      }
    }
    m[1][1] = true;
    for (i = 2; i &lt;= 24000; i++) {
      m[i][1] = true;
      for (j = 2; (j &lt;= i) &amp;&amp; (i * j &lt;=24000); j++) {
        for (l = 1; l &lt;= i; l++) {
      if (m[i][l]) {
        m[i * j][l + 1 + (i * j) - i - j] = true;
      }
        }
      }
    }
    for (i = 0; i &lt;= 24000; i++) {
      for (j = 0; j &lt;= 12000; j++) {
        if (m[i][j] &amp;&amp; (answer[j] == 0)) {
      answer[j] = i;
        }
      }
    }
    for (i = 2; i &lt;= 12000; i++) {
      cout &lt;&lt; answer[i] &lt;&lt; endl;
      all.insert(answer[i]);
    }
    cout &lt;&lt; all.size() &lt;&lt; endl;
    for (set&lt;long long&gt;::iterator it = all.begin(); it != all.end(); it++) {
      //cout &lt;&lt; *it &lt;&lt; endl;
      s += *it;
    }
    cout &lt;&lt; s &lt;&lt; endl;
  }
  </code></pre>
  
  <p>With the "useless" bool array, all the answers are right, between 0 and 24000; but without it, some answers in the middle got corrupted and become very large numbers.</p>
  
  <p>I am completely confused now; why would that unused array affect the middle of the answer array?</p>
  
  <p>Thanks and sorry for the long code! I will be grateful if someone could edit the code into a better example, I simply son't know what is with the code.</p>