title: float* from C to C#
body: <p>I'm not truly a CS guy, so if any of you geniuses on here can point me in the right direction I'll be eternally grateful.</p>
  
  <p>I have a c-code command line function that used to write its results to file.  I converted it to return it's data via a float* array to a C++ program like such (to avoid constant file I/O):</p>
  
  <pre><code>float * mgrib(int argc, char **argv)
  </code></pre>
  
  <p>This worked perfectly.  I now need to get this into a C# program, and here's where things go haywire.  </p>
  
  <p>The first thing I did to avoid the char ** was to make the arguments a series of bool.  That worked fine if I allow it to still dump to file.  </p>
  
  <p>The problem is juggling the c-style float array in C#.  Within the c-code it was allocated with malloc.</p>
  
  <p>So here's everything I've tried with no success(I know the size of the array):</p>
  
  <ol>
  <li><p>Make a "free" function to export to call from C# to release the memory when I'm done with it.  After a few loops the C# crashes with no warning.</p></li>
  <li><p>Release the malloc from C# with Marshal.FreeCoTaskMem.  Same result.</p></li>
  <li><p>Move the float* to an argument and remove the c-code malloc.  (void mgrib(..., float* data,...)</p></li>
  </ol>
  
  <p>__a)Allocate it with Marshal.AllocCoTaskMem.  Free it with Marshal.FreeCoTaskMem.</p>
  
  <p>__b)Use Marshal.Copy to allocate.  Free it with Marshal.FreeCoTaskMem (Maybe this is wrong?)</p>
  
  <p>I've dabbled in just about everything I could find in the internet.  Please let me know if more info is necessary.  I'm hoping this just a simple concept that I'm missing.</p>