0

I've searched the web for a while but can't seem to find the answer: Have a look at this code:

cdef float [::1] Example_v1 (float *A, float *B, float *C) :
    cdef:
        float [8] out
        float [::1] out_mem
        int i

    ## < do some stuff >
    ## body
    ## < finish doing stuff >

    out_mem = out
    print( " what is out & out_mem type here ", type(out)  , type(out_mem) )    
    return out_mem

def run1(float [::1] A, float [::1] B, float [::1] C):
    return Example_v1( &A[0] , &B[0] , &C[0] )   

I can compile this in cython without any error. Then when it comes to using it in a python code, the information inside out_mem is junk while I have verify the out is correct if I used a print statement to check the result. I know other alternatives are defined out as a pointer or directly initialized out_mem with np.zeroes(8, dtype=np.float). I'm just curious why doesn't it work as it is.

Also as a related question. If I do switch to using pointer:

cdef:
    float *out = <float*> malloc( 8 * sizeof(float)) 
    float [::1] out_mem = <float [:8]> out

Will I need to do free(out) otherwise memory leak ?

Please help.

Ong Beng Seong
  • 196
  • 1
  • 11
  • also related: https://stackoverflow.com/a/34945214/5769463 – ead Apr 01 '20 at 08:43
  • I think the question I linked is more useful since it explains how to solve it in Cython. I'd tend to use `cython.view.array` rather than creating your own wrapper class like the accepted answer says. @ead's answer hopefully explains why it's happening – DavidW Apr 01 '20 at 17:01

0 Answers0