On Mon, Apr 18, 2016 at 5:04 PM, Ray Yang <
rayya...@gmail.com> wrote:
> Agree for the dynamically allocated string from C side. But in my example,
> I'm returning a "const char*" which is not dynamically allocated (in normal
> C program, it's statically embedded in the executable file). So on golang
> side, should I still call C.free? Is the memory still invisibly allocated in
> this case?
The cgo tool will not introduce any invisible memory allocation. If
you call malloc yourself then you should call free when done. If you
do not call malloc, you do not need to call free. Note that functions
like C.CString will call malloc for you, as documented.
Ian
> On Monday, April 18, 2016 at 2:26:05 AM UTC-7, Tamás Gulácsi wrote:
>>
>> 2016. április 18., hétfő 7:43:07 UTC+2 időpontban Ray Yang a következőt
>> írta:
>>>
>>> Thanks! I didn't realize that I can declare a *C.char variable directly.
>>>
>> You can. But as that is a pointer, it's main reason is to point somewhere
>> - for example to a previously allocated array (see C.CString).
>> But here you pass a _pointer_ to this pointer, so C side will change the
>> value of that pointer - this pointer. So this can be nil/NULL/0,
>> the allocation will happen on the C side.
>> So after all, you'll have to clear it up on the C side - either with
>> C.free, or with some side effect in the C API.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
golang-nuts...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.