title: generate dynamic length arrays in c++ and accessing it in python using ctypes
body: <p>I am a novice at c++, currently trying to create a c extension to a python library using ctypes. My main issue is that I am generating two unknown lengthed vectors inside the c code and don't know the best way to initialize variables on the python code side to interface with the c code. </p>
  
  <p>My approach is to initialize two double pointer float arrays on python side and point the vector data to them on the c side.</p>
  
  <p>my_python_code.py:</p>
  
  <pre><code>float_ptr_ptr = ctypes.POINTER(ctypes.POINTER(ctypes.c_float))
  
  # initialize the array getter function
  dll = ctypes.cdll.LoadLibrary("my_c_lib.so")
  dll.get_arrays.argtypes = [float_ptr_ptr, float_ptr_ptr]
  dll.get_arrays.restype = ctypes.c_int
  
  # initialize the function that deletes float array data
  dll.delete_float_array.argtypes = [float_ptr_ptr]
  dll.delete_float_array.restype = None
  
  # initialize double pointer to float arrays
  output_arr1_py = ctypes.POINTER(ctypes.c_float)()
  output_arr2_py = ctypes.POINTER(ctypes.c_float)()
  
  # call c function and save it to numpy arrays
  vec_size = dll.get_arrays(ctypes.byref(output_arr1_py), ctypes.byref(output_arr2_py))
  np_arr1 = np.fromiter(output_arr1_py, dtype=np.float, count=vec_size.value)
  np_arr2 = np.fromiter(output_arr2_py, dtype=np.float, count=vec_size.value)
  
  # deallocate float arrays in c
  dll.delete_float_array(output_arr1_py)
  dll.delete_float_array(output_arr2_py)
  </code></pre>
  
  <p>On c++ side I have a function <code>get_arrays()</code> that takes in two double-pointer to float array. <code>get_arrays</code> calls <code>set_vector_values</code> which populates two float vectors (<code>output_vec_1</code>, <code>output_vec_2</code>) and I want to then copy the data from <code>output_vec_1</code> to <code>output_arr_1</code> and <code>output_vec_2</code> to <code>output_arr_2</code>. I have two implementations:</p>
  
  <p>my_c_lib.cc (implementation 1):</p>
  
  <pre><code>int get_arrays(float **output_arr_1, float **output_arr_2) {
      std::vector&lt;float&gt; output_vec_1; # same size as output_vec_2
      std::vector&lt;float&gt; output_vec_2;
  
      if (!set_vector_values(&amp;output_vec_1, &amp;output_vec_2)) {return -1; # if set_vector_values fails exit with retcode -1}
      *(output_arr_1) = output_vec_1.data();
      *(output_arr_2) = output_vec_2.data();
      return output_vec_1.size();
  }
  </code></pre>
  
  <p>my_c_lib.cc (implementation 2):</p>
  
  <pre><code>int get_arrays(float **output_arr_1, float **output_arr_2) {
      std::vector&lt;float&gt; output_vec_1; # same size as output_vec_2
      std::vector&lt;float&gt; output_vec_2;
  
      if (!set_vector_values(&amp;output_vec_1, &amp;output_vec_2)) {return -1; # if set_vector_values fails exit with retcode -1}
      float *tmp1 = new float[output_vec_1.size()];
      for (int32_t i = 0; i &lt; output_vec_1.size(); ++i) {
          tmp1[i] = output_vec_1[i];
      }
  
      float *tmp2 = new float[output_vec_2.size()];
      for (int32_t i = 0; i &lt; output_vec_2.size(); ++i) {
          tmp2[i] = output_vec_2[i];
      }
  
      *(output_arr_1) = tmp1;
      *(output_arr_2) = tmp2;
  
      return output_vec_1.size();
    }
  
  void delete_float_array(float **arr){
      delete[] *(arr);
   }
  </code></pre>
  
  <p>From my understanding c++ automatically handles memory de-allocation of vectors if I go out of scope how does this affect pointers to <code>vector.data()</code>? For implementation 1 for example, would <code>output_arr_1</code> still point to <code>output_vec_1.data()</code> or did that disappear once I move out of scope from <code>get_arrays</code>?</p>
  
  <p>implementation 2 is me trying to deep copy the vector data into a temp float array so that output_arr_1 and _2 does not point to the vectors' data. once I am done with using the array I will then call <code>delete_float_array</code> to free up the array memory to prevent memory leak.</p>
  
  <p>please let me know if there are better ways of doing what I am trying to accomplish.</p>