title: 2d boolean array initialization in c++
body: <p>I don't use C that much and I recently got confused about 2d array initialization problem. I need to debug somebody's code and stuck in the following(her original code):</p>
  
  <pre><code>const int location_num = 10000;
  bool **location_matrix;
  if (node_locations)
      {
          location_matrix = (bool **)malloc(location_num*sizeof(bool *));
          if (!location_matrix)
          {
                  cout&lt;&lt;"error 1 allocating location_matrix" &lt;&lt; endl;
                  exit;
          }
          for (i=0; i&lt;location_num; i++)
          {
                  location_matrix[i] = (bool *) malloc(location_num*sizeof(bool ));
                  if (!location_matrix[i])
                  {
                          cout&lt;&lt;"error 2 allocating location_matrix" &lt;&lt; endl;
                          exit;
                  }
                  for (j=0; j&lt;location_num; j++)
                          location_matrix[i][j] = false;
          }
      }
  </code></pre>
  
  <p>I thought is was redundant, so I changed it to the following:</p>
  
  <p><code>location_matrix[location_num][location_num] = { {false} };</code></p>
  
  <p>However, segmentation fault happens at runtime. 
  My question is: how does the above code fail? If it looks right, what's the difference between dynamically allocation and static allocation? Is it just because the dimension might not be constant, so we need to do it dynamically?
  Also, just for curiosity, how do I malloc 2d array that stores pointers? Thanks.</p>