title: c++11 using std::fill to fill a 2D dynamically allocated array
body: <p><strong>A discussion along this line could be found in this <a href="https://stackoverflow.com/questions/3948290/whats-the-safe-way-to-fill-multidimensional-array-using-stdfill">question</a> and also in <a href="https://stackoverflow.com/questions/25158380/is-it-possible-to-use-stdfill-with-a-2d-c-style-array">here</a>, but my case is slightly different, as I am dealing with a dynamically allocated memory.</strong></p>
  
  <p><strong>also please note, <code>memset</code> does not quite work with <code>double</code> value.</strong></p>
  
  <p>Anyway, I am trying to use <code>std::fill</code> to fill a dynamically allocated 2D array --</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  
  using std::cout ; using std::endl ;
  using std::fill ;
  
  int main()
  {
      double **data ;
      int row = 10, col = 10 ;
      data = new double*[row] ;
      for(int i = 0 ; i &lt; col ; i++)
          data[i] = new double[col];
  
      // fill(&amp;(data[0][0]), 
      //      &amp;(data[0][0]) + (row * col * sizeof(double)), 1); // approach 1
      // fill(&amp;(data[0][0]), &amp;(data[0][0]) + (row * col), 1);   // approach 2
      // fill(data, data + (row * col * sizeof(double)), 1);    // approach 3
      // fill(&amp;(data[0][0]), 
      //      &amp;(data[0][0]) + 
      //       ((row * sizeof(double*)) + 
      //        (col * sizeof(double))), 1);                    // approach 4
  
      for(int i = 0 ; i &lt; row ; i++) {
          for(int j = 0 ; j &lt; col ; j++)
              cout &lt;&lt; data[i][j] &lt;&lt; " " ;
          cout &lt;&lt; endl ;
      }
  
      for(int i = 0 ; i &lt; row ; i++)
          delete [] data[i] ;
      delete [] data ;
  }
  </code></pre>
  
  <p><strong>Approach 1:</strong> What I understand, the approach 1 should be the correct code, I am starting from the beginning <code>&amp;(data[0][0])</code> and the end of the array should be located at <code>&amp;(data[0][0]) + (row * col * sizeof(double))</code>, but when I run, I get this error, but the array has been filled --</p>
  
  <pre><code>1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  *** Error in `./test': free(): invalid next size (fast): 0x0000000000da3070 ***
  Aborted (core dumped)
  </code></pre>
  
  <p><strong>Approrach 2:</strong> However, according to this <a href="https://stackoverflow.com/questions/3948290/whats-the-safe-way-to-fill-multidimensional-array-using-stdfill">post</a>, the approach 2 is recommended, but I do not quite understand this code, since <code>sizeof(double)</code> is missing, and I am getting this output --</p>
  
  <pre><code>1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
  1 1 1 1 0 0 0 0 0 0 
  0 0 0 0 0 0 0 0 0 0 
  *** Error in `./test': free(): invalid next size (fast): 0x0000000000bf5070 ***
  Aborted (core dumped)
  </code></pre>
  
  <p><strong>Approach 3:</strong> I am not sure why this does not compile, <code>data</code> and <code>&amp;(data[0][0])</code> should have the same meaning, right?</p>
  
  <p><strong>Approach 4:</strong> I am not sure if this is correct or not.</p>
  
  <ol>
  <li>How do I do it ? </li>
  <li>Does <code>std::fill</code> give any extra benefit over two nested loops?</li>
  </ol>