Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cgo: How to pass struct array from c to go

Tags:

go

cgo

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?

like image 372
crackcell Avatar asked Mar 08 '15 09:03

crackcell


1 Answers

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.

like image 163
JimB Avatar answered Nov 08 '22 01:11

JimB