简体   繁体   中英

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!

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

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 (ie 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM