-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Description
Hey guys,
I'm in trouble that how to pass a a two-dimensional integer array to C. My code as the following, the String
slice works well, but the int
two-dimensional array failed!
C code:
bool test_settopologyresource(bsagomodule* _self, char* _nodename,
int _resourceindex, char** _nameunits, int** _levelmatrix) {
printf("\n set topology resource-> node:%s, resource index:%d", _nodename, _resourceindex);
bool result = settopologyresource(_self->client, _nodename, _resourceindex, _nameunits, _levelmatrix);
return result;
}
Go code:
gonameunits := []string{"gpu0", "gpu1", "gpu2", "gpu3"}
nameunits := make([]*C.char, len(gonameunits))
for i, _ := range gonameunits {
nameunits[i] = C.CString(gonameunits[i])
defer C.free(unsafe.Pointer(nameunits[i]))
}
fmt.Println("nameunits:", nameunits)
golevelmatrix := [][]int{{1}, {3, 3}, {3, 3, 2}}
levelmatrix := make([][]C.int, len(golevelmatrix))
for i, _ := range golevelmatrix {
levelmatrix[i] = make([]C.int, len(golevelmatrix[i]))
for j, _ := range golevelmatrix[i] {
levelmatrix[i][j] = C.int(golevelmatrix[i][j])
}
}
fmt.Println("levelmatrix:", levelmatrix)
C.test_settopologyresource(mod, C.CString("node1"), C.int(2), (**C.char)(unsafe.Pointer(&nameunits[0])), (**C.int)(unsafe.Pointer(&levelmatrix[0][0])))
I also test only pass a one-dimensional integer array, it works well! So, how to pass a two-dimensional integer array to C from Go by cgo? Or something else I missed?