Export function that returns array of doubles

前端 未结 1 342
暗喜
暗喜 2021-01-24 13:38

In Golang how to export the function that returns array of doubles. The way it was possible before seems to return \"runtime error: cgo result has Go pointer\" now:



        
相关标签:
1条回答
  • 2021-01-24 14:31

    In order to safely store a pointer in C, the data it points to must be allocated in C.

    //export Init
    func Init(f string) (C.size_t, *C.double) {
        size := 10
    
        // allocate the *C.double array
        p := C.malloc(C.size_t(size) * C.size_t(unsafe.Sizeof(C.double(0))))
    
        // convert the pointer to a go slice so we can index it
        doubles := (*[1<<30 - 1]C.double)(p)[:size:size]
        doubles[3] = C.double(1.5)
    
        return C.size_t(size), (*C.double)(p)
    }
    
    0 讨论(0)
提交回复
热议问题