On Tue, Jan 30, 2018 at 9:13 PM, hǎi sū <uor...@gmail.com> wrote: > 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) > } > } > ```
len(a) is the length in bytes, which is 178209. But the C code is using that length to index into a short*, meaning that it is looking at byte 356418, which is far past the end of the array. The C code should take unsigned char * or you should divide len(a) by 2 or something. 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.