> I have a C function that returns me a fairly huge char array (I know
How huge, just to have an idea?
> the size of the array, of course). I'd like to wrap this function
> using cgo, and avoid possible overheads (cpu, memory); is there a way
> to do this? Any work-arounds?
I'd recommend starting with something trivial: just copy the array
once into a []byte using a normal conversion, and then use that to
pass around in Go. If you figure the penalty of copying between C and
Go is really creating issues, you can play with the unsafe package in
a few different ways (some less safe than others).
Can you provide the array (memory) for the C function to work with, or
does the library allocate and provide it to you?
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter
Yes, you can do that using unsafe.Pointer and a type like
reflect.SliceHeader. Be aware that you'll have to manage the memory
of the array entirely by hand, and it will explode in unfriendly ways
if you don't deal with it properly. This is also unsafe, and not
guaranteed to work in the future.
That won't work. The GC won't manage that memory region, so there are
no finalizers there either.