1

I am building a Python wrapper to a C code.

In the .c code I have the following defined function

double myfunction_at_k_and_z(struct background * pba,
                      struct spectra * psp,
                      struct perturbs *ppt,
                      double k,
                      double z,  
                      double * pk_ic)
{ ...body of the function ...}

and in the .h file I have

  struct background ba;       
  struct perturbs pt;         
  struct spectra sp;          

Now I implement a function in my .pyx file, inside a class myclass

def get_myfunction(self, double k, double z):
        cdef double dummy
        result = myfunction_at_k_and_z(&self.ba, &self.sp, &self.pt, k, z, &dummy)
        return result

and in the .pxd file I write:

cdef struct background: ....
cdef struct spectra: ...
cdef struct perturbs: ...


double myfunction_at_k_and_z (background * pba, spectra * psp, perturbs *ppt, double k, double z, double * pk_ic)

where the dots ... denote the components of the structures, which are not relevant here apart from one case: in struct spectra there are

int ln_k_size
double * ln_k

Now my problem is that despite everything compiling, when I run with Python my wrapper and use get_myfunction through e.g. myclass.get_myfunction(1., 1.), there seems to be a problem with the array ln_k, which apparently seems to have ln_k_size=0. This causes segmentation error.

Am I missing something in terms of allocation of memory? Any way out?

johnhenry
  • 1,293
  • 5
  • 21
  • 43

0 Answers0