This program reports only one allocation per operation, which seems
to indicate that your solution is already the right one:

https://play.golang.org/p/Fx1EMYKRRd


On 1 February 2017 at 09:18, T L <tapir....@gmail.com> wrote:
> Sometimes, I want to create a new string which will not share the underlying
> bytes with other strings.
> For example, to avoid the substring memory leak problem.
>
> newStr := string([]byte(oldStr)) is simple but will make two memory
> allocations, and one of them is a waste.
>
> There are two solutions which only make one memory allocation in
> https://groups.google.com/forum/#!topic/golang-nuts/naMCI9Jt6Qg
> (code: https://play.golang.org/p/3rVsMTaHUz).
> But in that thread, someone said the two are not safe from being optimized
> unexpectedly in later compiler versions.
>
> I found the most approached function in the standard lib is
> strings.Repeat(), but it also makes two memory allocations.
>
> Is there a safe way to do this with only one memory allocation?
>
>
>
> --
> 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.

-- 
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.

Reply via email to