Why is passing a pointer to a pointer in cgo dissalowed?

150 views
Skip to first unread message

pierspo...@gmail.com

unread,
Jan 23, 2017, 11:13:32 AM1/23/17
to golang-nuts
The doc for cgo states ...

"Go code may pass a Go pointer to C provided the Go memory to which it points does not contain any Go pointers. The C code must preserve this property: it must not store any Go pointers in Go memory, even temporarily. When passing a pointer to a field in a struct, the Go memory in question is the memory occupied by the field, not the entire struct. When passing a pointer to an element in an array or slice, the Go memory in question is the entire array or the entire backing array of the slice.

Why is this? Is it because go only keeps track of the pointers that are direct arguments to a cgo call.  So if for instance a struct containing the only pointer to some value is passed to cgo then go loses track of that reference and the memory pointed at will be up for garbage collection?

Thanks,

Piers



Ian Lance Taylor

unread,
Jan 23, 2017, 12:43:56 PM1/23/17
to pierspo...@gmail.com, golang-nuts

pierspo...@gmail.com

unread,
Jan 24, 2017, 7:29:23 AM1/24/17
to golang-nuts, pierspo...@gmail.com
Thanks for the links Ian, very informative :)
Reply all
Reply to author
Forward
0 new messages