reckoner reckoner - 1 month ago 21
C++ Question

How to handle double pointer in C++ wrapping by Cython

I have a C++ object with this signature,

Foo(const bool* const*, const int, const float=0.025);


I have the corresponding a PYX file

import numpy as np
cimport numpy as np

cdef extern from "foo.h":
cdef cppclass Foo:
Foo()
Foo(int**, const int, const float=0.025)
void mcq(int* &maxclique, int &sz)

cdef class MyFoo:
cdef Foo c_mxclik

def __cinit__(self, int[:] b, int k, float x):
self.c_mxclik = Foo(b,k,x)


which doesn't work. I don't understand how to get the
const bool* const*
part of the input from Python to the C++ code via Cython.

I'm stuck. Thanks!

Answer

Firstly, int and bool and not generally the same size, so it's probably dangerous to pass an array of ints to a function that expects an array of bools.

I'd suggest you change your Cython code to match the C++ signature. You can access the C++ bool in Cython with

from libcpp cimport bool

Second, you just need to take the address using the C-style & operator. You need to do it in two steps (i.e. first set up a bool*, and then take the address of that).

def __cinit__(self, bool[:] b, int k, float x):
    cdef bool* b_ptr = &b[0] # address of first element
    self.c_mxclik = Foo(&b_ptr,k,x)