Hey Guys, I'm in trouble in the same issue. My code as the following:
test.go ```go name := []string{"gpu0", "gpu1", "gpu2", "gpu3"} matrix := [3][3]int{{1, 0, 0}, {3, 3, 0}, {3, 3, 2}} C.test_settopologyresource(mod, C.CString("node1"), C.int(2), (**C.char)(unsafe.Pointer(&name[0])), (**C.int)(unsafe.Pointer(&matrix[0][0]))) ``` xxx.c ```c 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; } ``` But when I execute `go run test.go`, got the below errors: ``` go run test.go fatal error: unexpected signal during runtime execution [signal SIGSEGV: segmentation violation code=0x1 addr=0xc pc=0x7f5e655089da] runtime stack: runtime.throw(0x4b1190, 0x2a) /usr/local/go/src/runtime/panic.go:596 +0x95 runtime.sigpanic() /usr/local/go/src/runtime/signal_unix.go:274 +0x2db goroutine 1 [syscall, locked to thread]: runtime.cgocall(0x481400, 0xc42004f668, 0x4b0f76) /usr/local/go/src/runtime/cgocall.go:131 +0xe2 fp=0xc42004f628 sp=0xc42004f5e8 main._Cfunc_bsagomodule_settopologyresource(0x1226440, 0x122d2f0, 0x2, 0xc42001c280, 0xc42005c050, 0x122d200) command-line-arguments/_obj/_cgo_gotypes.go:174 +0x4a fp=0xc42004f668 sp=0xc42004f628 main.main.func8(0x1226440, 0x122d2f0, 0x2, 0xc42001c280, 0xc42005c050, 0x0) /home/zhangjian/project/k8s_bsamodule/test/test.go:94 +0xf4 fp=0xc42004f6a8 sp=0xc42004f668 main.main() /home/zhangjian/project/k8s_bsamodule/test/test.go:96 +0xeca fp=0xc42004ff88 sp=0xc42004f6a8 runtime.main() /usr/local/go/src/runtime/proc.go:185 +0x20a fp=0xc42004ffe0 sp=0xc42004ff88 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc42004ffe8 sp=0xc42004ffe0 goroutine 17 [syscall, locked to thread]: runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2197 +0x1 exit status 2 ``` Have any ways to solve this problem? Thanks a lot! 在 2015年12月10日星期四 UTC+8上午3:07:17,Ian Lance Taylor写道: > > On Wed, Dec 9, 2015 at 6:27 AM, Cobb Liu <keyc...@gmail.com <javascript:>> > wrote: > > My code is : > > > > package main > > > > /* > > #include <stdio.h> > > #include <string.h> > > > > void fill_2d_array(char (*s)[16]) { > > strcpy(s[0], "hello"); > > strcpy(s[1],"cgo"); > > } > > */ > > import "C" > > import "fmt" > > import "unsafe" > > > > func main() { > > dirs := make([][]byte, 4) > > for i := 0; i < 4; i++ { > > dirs[i] = make([]byte, 16) > > } > > C.fill_2d_array(((*C.char)[16])(unsafe.Pointer(&dirs))) > > > > fmt.Println(dirs) > > } > > > > when I run with 'go run test.go', it failed and said: > > # go run test.go > > ./test.go:21: type *C.char is not an expression > > > > > > How can I pass a 2 dimensional slice to a C function like > 'fill_2d_array' > > above as a parameter? > > This code looks wrong at several levels. > > It looks like you are trying to write a Go type (*C.char)[16]. That's > not a Go type. A Go type would be [16]*C.char. But you need a > pointer, so you probably want *[16]*C.char. But that doesn't match > your C code. Your C code is expecting a pointer to an array of 16 > chars, which in Go would be written as *[16]C.char. The strcpy to > s[0] is working because in C arrays decay to pointers, so the C code > is effectively **C.char and s[0] is *C.char. But none of this matches > what Go is passing, since a slice is not a pointer. > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.