std::string
allocates dynamic memory at runtime for its character data (unless the std::string
implementation employs "Short String Optimization" and the data is short enough to fit in the SSO buffer).
So, one scenario you may want to use C-style strings for is when you want to pass around string literals without allocating memory for them. Assigning a string literal to a std::string
will allocate dynamic memory (if SSO is not used).
There can also be scenarios where you want to process character data without allocating new memory for extracted substrings. C-style strings can be good for that, too (though std::string_view
in C++17 and later would generally be better for that).