the code is below. ``` package main import "math/rand" import "fmt"
/* #include <stdio.h> #include <stdlib.h> void t(const short *buf, size_t buf_size) { printf("in t."); printf("0: %d ", buf[0]); printf("buf_size-1: %d\n", buf[buf_size-1]); } */ import "C" func main() { for { a := make([]byte, 178209) rand.Read(a) fmt.Println(a[0], a[len(a)-1]) aPointer := C.CBytes(a) C.t((*C.short)(aPointer), C.size_t(len(a))) C.free(aPointer) } } ``` And result is ``` root@ieltscms-v183-tp9xn:/# go run main.go 82 59 in t.0: -686 buf_size-1: 0 111 248 fatal error: unexpected signal during runtime execution [signal SIGSEGV: segmentation violation code=0x1 addr=0x1279850 pc=0x48ac2b] runtime stack: runtime.throw(0x4c07c2, 0x2a) /usr/local/go/src/runtime/panic.go:605 +0x95 fp=0x7fff87b66950 sp=0x7fff87b66930 pc=0x428ba5 runtime.sigpanic() /usr/local/go/src/runtime/signal_unix.go:351 +0x2b8 fp=0x7fff87b669a0 sp=0x7fff87b66950 pc=0x43bc38 goroutine 1 [syscall, locked to thread]: runtime.cgocall(0x48ac60, 0xc420067f00, 0x0) /usr/local/go/src/runtime/cgocall.go:132 +0xe4 fp=0xc420067ec0 sp=0xc420067e80 pc=0x402e34 main._Cfunc_t(0x1222810, 0x2b821) command-line-arguments/_obj/_cgo_gotypes.go:67 +0x45 fp=0xc420067f00 sp=0xc420067ec0 pc=0x48a795 main.main() /main.go:25 +0xd7 fp=0xc420067f80 sp=0xc420067f00 pc=0x48a967 runtime.main() /usr/local/go/src/runtime/proc.go:195 +0x226 fp=0xc420067fe0 sp=0xc420067f80 pc=0x42a296 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc420067fe8 sp=0xc420067fe0 pc=0x452b11 goroutine 2 [force gc (idle)]: runtime.gopark(0x4c1c00, 0x732ac0, 0x4bbfc4, 0xf, 0x14, 0x1) /usr/local/go/src/runtime/proc.go:287 +0x12c fp=0xc420044768 sp=0xc420044738 pc=0x42a73c runtime.goparkunlock(0x732ac0, 0x4bbfc4, 0xf, 0xc420000114, 0x1) /usr/local/go/src/runtime/proc.go:293 +0x5e fp=0xc4200447a8 sp=0xc420044768 pc=0x42a82e runtime.forcegchelper() /usr/local/go/src/runtime/proc.go:245 +0xcc fp=0xc4200447e0 sp=0xc4200447a8 pc=0x42a55c runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc4200447e8 sp=0xc4200447e0 pc=0x452b11 created by runtime.init.4 /usr/local/go/src/runtime/proc.go:234 +0x35 goroutine 3 [GC sweep wait]: runtime.gopark(0x4c1c00, 0x732bc0, 0x4bbb8d, 0xd, 0x41df14, 0x1) /usr/local/go/src/runtime/proc.go:287 +0x12c fp=0xc420044f60 sp=0xc420044f30 pc=0x42a73c runtime.goparkunlock(0x732bc0, 0x4bbb8d, 0xd, 0x14, 0x1) /usr/local/go/src/runtime/proc.go:293 +0x5e fp=0xc420044fa0 sp=0xc420044f60 pc=0x42a82e runtime.bgsweep(0xc42002e070) /usr/local/go/src/runtime/mgcsweep.go:52 +0xa3 fp=0xc420044fd8 sp=0xc420044fa0 pc=0x41dfc3 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc420044fe0 sp=0xc420044fd8 pc=0x452b11 created by runtime.gcenable /usr/local/go/src/runtime/mgc.go:216 +0x58 goroutine 4 [finalizer wait]: runtime.gopark(0x4c1c00, 0x750eb0, 0x4bbdd0, 0xe, 0x14, 0x1) /usr/local/go/src/runtime/proc.go:287 +0x12c fp=0xc420045700 sp=0xc4200456d0 pc=0x42a73c runtime.goparkunlock(0x750eb0, 0x4bbdd0, 0xe, 0x14, 0x1) /usr/local/go/src/runtime/proc.go:293 +0x5e fp=0xc420045740 sp=0xc420045700 pc=0x42a82e runtime.runfinq() /usr/local/go/src/runtime/mfinal.go:175 +0xb8 fp=0xc4200457e0 sp=0xc420045740 pc=0x415028 runtime.goexit() /usr/local/go/src/runtime/asm_amd64.s:2337 +0x1 fp=0xc4200457e8 sp=0xc4200457e0 pc=0x452b11 created by runtime.createfing /usr/local/go/src/runtime/mfinal.go:156 +0x62 signal: aborted (core dumped) ``` I pass the `aPointer` to c function `t`. And it will panic, after the 1st loop. And I try move `a := make([]byte, 178209)` to outside of for loop. The program work fine. Could anyone tell me why this happened? thx. -- 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.