Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
This actually allocates, copies and deletes 1000 heap buffers with the string "hello"! The problem here is that "hello" is ''not'' an instance of `stdstd::string`string. It is a char\[5\] that must be converted to a temporary std::string using the appropriate constructor. However std::string always wants to manage its own memory, so the constructor allocates a new buffer and copies the string. Once f() returns and we go round the loop again the temporary is deleted along with its buffer.

...

This time we have a constant `stdstd::string` string that is created once at start up and destroyed once at shut-down. The anonymous namespace makes the constant private to this ` .cpp` cpp file so we wont have name clashes. (Its similar to using the `static` static keyword on a global declaration in C, but anonymous namespaces are the preferred way to do it in modern C++)