7

The C part:

struct Person {...}
struct Person * get_team(int * n)

The Go part:

n := C.int(0)
var team *C.struct_Person = C.get_team(&n)
defer C.free(unsafe.Pointer(team))

I can get the first element of the array in this way. But how to get the whole array with n elements? and how to free them safely?

crackcell
  • 293
  • 2
  • 8
  • You probably want/need to covert the C pointer (and implied length n??) into a Go slice. That is non-trivial, perhaps some support code using reflection? – Dave C Mar 08 '15 at 18:41

1 Answers1

15

First, even though you’re using Go, when you add cgo there is no longer any "safe". It's up to you to determine when and how you free the memory, just as if you were programming in C.

The easiest way to use a C array in go is to convert it to a slice through an array:

team := C.get_team()
defer C.free(unsafe.Pointer(team))
teamSlice := (*[1 << 30]C.struct_Person)(unsafe.Pointer(team))[:teamSize:teamSize]

The max-sized array isn't actually allocated, but Go requires constant size arrays, and 1<<30 is going to be large enough. That array is immediately converted to a slice, with the length and capacity properly set.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • Nice! It works! But I cant get the array slicing part [:teamSize:teamSzie] . I replace it with [:teamSize], it works the same. Why double teamSize? – crackcell Mar 09 '15 at 06:07
  • @crackcell: The third field in slice notation (the first is 0, which you can omit here) sets the slice capacity ([Slice Expressions](http://golang.org/ref/spec#Slice_expressions)) . You need it to prevent a possible buffer overrun into memory outside that of your array. – JimB Mar 09 '15 at 12:48
  • 1
    @JimB: Now that I have enough reputation to write a comment: Could you please correct your answer and add a `defer` before `C.free` (see my answer to this question for the other alternative). The way you wrote it, memory is freed before usage, i.e. invalid memory accesses are guaranteed when using `teamSlice`. – Pedro Dec 06 '17 at 09:43
  • Shouldn't it be `C.struct_Person` instead of `C.team` in the last line of the code snippet? This works for me `teamSlice := (*[1 << 30]C.struct_Person)(unsafe.Pointer(team))[:teamSize:teamSize]`. – ricpacca Oct 05 '18 at 22:13