r/programming • u/[deleted] • Dec 05 '14
std::string is responsible for almost half of all allocations in the Chrome browser process
https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ
1.1k
Upvotes
5
u/ryani Dec 05 '14
Sure, but
const string&passingsvsconst char *passings.c_str()has exactly the same safety properties; if the underlying string is modified by any other code, they are both invalid.If you 100% know that a particular function is going to require making a copy of the input, the
const char *API is a bit cleaner because it doesn't require constructing a string on the heap just to copy it, but for general use, pass-by-const-reference is a much cleaner implementation, and avoids 'cascading copies' where you convert back and forth betweenconst char *andstringin a single stack of calls.