On Wed, Sep 4, 2013 at 2:54 PM, Luke Scott <
lu...@visionlaunchers.com> wrote:
> Who is responsible for clearing "char *p" or "void *data"? If I am
> responsible, when? Where - from C free(), or from Go C.free()? After the
> function call? Does Go make a copy of the memory?
It depends on who allocated the memory.
If Go allocated the memory, it is in an area managed by the garbage
collector, and thus it will be deallocated automatically at some point
after the last reference in Go land is dead. That means you cannot
trust the string to be alive in C land, unless you're sure there's a
reference in Go land to the same string.
If C allocated the memory, it must be explicitly freed. That said, Go
has no idea about whether the memory is there or not.. it will simply
trust it to be there for as long as the string is alive, because
that's the normal behavior with strings it allocates internally. If
you explicitly free the memory backing the string while there is still
a references to in Go land, the program will crash or misbehave
arbitrarily when the string is touched again (and people will be very
angry :-).
Note that this is very implementation dependent. Although these
details are unlikely to change imminently, they can change without
much notice.
> If GoString and GoSlice is allocated to the heap instead of the C function
> stack, do I need to clear this as well? What if I have a []string that needs
> to be constructed?
The details are similar to the above.
> Lastly, how do I construct a []interface{}? The []interface{} needs to
> contain string, int, uint, bool, and C.object. Would I be better off using
> unsafe.Pointer with uintptr to transverse the "array" of items (using a
> union struct, or something)?
I would do that from Go instead of doing it from C.
gustavo @
http://niemeyer.net