On Wed, 19 Apr 2017 14:23:09 +0800 hui zhang <fastfad...@gmail.com> wrote:
> 1) getpath() return a temp string, its c_str() pointer will be > free out of function. So I use malloc I'm not completely sure you're correct. C++ does not implement garbage collection and the object on which you have called c_str() continues to live on after getpath() returns. Now let's cite the manual on c_str(): | The pointer obtained from c_str() may be invalidated by: | * Passing a non-const reference to the string to any standard library | function, or | * Calling non-const member functions on the string, excluding | operator[], at(), front(), back(), begin(), rbegin(), end() and | rend(). So I'd say in the sequence of calls I propose there's no chance of anything quoted to happen -- basically the pointer returned by c_str() gets passed to Go where C.GoString() copies the memory pointed to by it. Things may break if you somehow concurrently call into your C++ side and those calls may access the same std::string object we're talking about. But if you do this, all bets are already off. > 2) Assume we use the malloc way , are C.GoString() copying the > pointer memory ? for we need C.free() malloc memory. Yes, C.GoString() copies the memory. Yes, you always need to free() what was malloc()ed for you -- because you own the returned pointer. -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.