On 29 December 2010 01:15, Hargett, Matt wrote: > > 1) allocation of std::string in local variable > 2) calls to said local string's c_str() method within loops > 3) and said loops do not modify the contents of the value returned from > c_str()
You can't modify the contents, c_str() returns a const char* > Example: > > #include <string> > #include <cstdio> > > void notify(const char* printable) { printf("%s\n", printable); } > > int main(void) > { > std::string name("bob"); > for (int i = 0; i < name.length(); i++) > { > notify(name.c_str()); What would you do to improve performance here? I don't see any significant opportunity for optimisation here. The second idea is obviously useful, as it could avoid unnecessary heap allocations (although not in C++0x where strings are not going to be reference-counted.)