title: Memory issue reading a .csv file and allocating the data into a multi-dimensional array using C
body: <p>I should preface this by saying I have absolutely zero experience with C. However, I need to use it to write a UDF for a CFD simulation I'm doing.</p>
  <p>My problem is that I have to read a .csv file and import those values to a 2d array so I can then access those values during the simulation. I have managed to get the code to read and print the data from the .csv file correctly. However, it all breaks when I try to convert and then add that data to an array.</p>
  <p>I am using strtod() to convert the read data. It seems that this conversion works and gives correct values. However, when I add code to allocate it to the array, the code fails to read the whole .csv file, stopping about halfway through. I think this has to do with a memory issue (not sure though), hence why I tried to play around with pointers and the free() function. Although this doesn't seem to help at all. In fact, it just throws errors &quot;attempt to free a non-heap object.&quot;</p>
  <p>Could anyone show me where I'm going wrong?</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
      char str[128];
      int result;
      int width = 2;
      int csvlength = 224;
      double d;
      double data[csvlength][width];
      int counter=0;  // counter to move between fields
  
      FILE* f = fopen(&quot;vindisc.csv&quot;, &quot;r&quot;);  // instantiate file object as pointer to allow for dynamic size.
  
      do {
          result = fscanf(f, &quot;%127[^,\n]&quot;, str);  // get the result
  
          /* If it is end of line, skip */
          if(result == 0)
          {
              result = fscanf(f, &quot;%*c&quot;);
          }
  
          /* otherwise, read in the data */
          else
          {
              if (counter%2 == 0) {
                  printf(&quot;Field_1: %s\n&quot;, str);  
                  d = strtod(str, NULL);
                  //printf(&quot;Double test 1: %g\n&quot;, d);
                  data[counter][0] = d;
              }
              else {
                  printf(&quot;Field_2: %s\n&quot;, str);
                  d = strtod(str, NULL);
                  //printf(&quot;Double test 2: %g\n&quot;, d);
                  data[counter][1] = d; 
              }
              
              counter++;  // update the counter
          }
          //free(&amp;str);
      } while(result != EOF);  // close the file after the read.
  
      for(int i = 0; i &lt; csvlength; i++) {
          printf(&quot;%g&quot;, data[i][0]);
          printf(&quot;\n&quot;);  
          printf(&quot;%g&quot;, data[i][1]);
          printf(&quot;\n&quot;);   
      }
      return 0;
  }
  </code></pre>
  <p>Added an extract from the csv file to show the structure.</p>
  <p><a href="https://i.stack.imgur.com/7Xr8k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Xr8k.png" alt="" /></a></p>