It doesn't seem like you can do array new (new something[n]) in Cython. The simplest solution would be to create 1-line C++ function to do array new and array delete, and call those instead.
template <typename T>
T* array_new(int n) {
return new T[n];
}
template <typename T>
void array_delete(T* x) {
delete [] x;
}
Called from the following Cython file
# distutils: language = c++
from libcpp.vector cimport vector
ctypedef vector[double] dvec
cdef extern from "cpp_funcs.hpp":
T* array_new[T](int)
void array_delete[T](T* x)
def example(int n):
cdef dvec* arr = array_new[dvec](n)
try:
if n>0:
arr[0].push_back(10)
print(arr[0][0])
finally:
array_delete(arr)
Given that Cython's C++ support is limited, and awkward in places (and given that you have to have a pretty good understanding of C++ to use it anyway) I think that writing a small amount of C++ code is usually a reasonable solution, and can save quite a bit of time. Some people seem to want to avoid this at all costs though....
I'd still recommend using a vector of vectors (vector<vector<double>>) instead since you get the memory management for free. Even better would be to stick with Python types and use a list of numpy arrays, but that might not be suitable if you want to interface with external C++ code.
double *d = new(arr[0]) double, where you construct a new double in an already-allocated memory location. You're not doing that in your code, and I can't imagine what use it would have in or near this code.