std::string::c_str() use in a
std::string operationA string operation uses the output of an std::string::c_str
method, resulting in inefficient code
This defect occurs when a string operation is performed by using the C-string pointer
obtained from std::string::c_str. For instance, this checker is raised when:
A new std::string or std::wstring is
implicitly or explicitly constructed from the output of the corresponding
c_str. This situation arises when a function expecting a
const reference to the string encounters a const
char* instead.
A new copy of a string object is created explicitly from the output of its
c_str function. Using the copy constructor is the more efficient
way of copying the string object.
Certain std::string member functions are invoked by using the
output of std::string::c_str. Flagged functions include
replace, append, assign,
compare, and find. Using an
std::string object directly to invoke
std::string member functions is more efficient.
A user-defined function that is overloaded to accept either of the const
char* and const std::string arguments is invoked by using
a C-string pointer. It is more efficient to invoke the std::string
overload of such a function.
It is expensive and inefficient to use the C-string output of the
std::string::c_ctr method when you can use an
std::string object instead. An std::string object
contains the length of the string. When you use the C-string output of the
std::string::c_str method instead of an std::string
object, the constructor determines the length of the C-string by a linear search, resulting
in inefficient code. Using std::string::c_str is also often unnecessary.
Consider this
code:
void set_prop1(const char* str);
void set_prop2(const std::string& str);
void foo( std::string& str){
//...
set_prop1(str.c_str()); // Necessary
//...
set_prop2(str.c_str()); // Inefficient
}foo calls two different functions. Because the function
set_prop1 requires a C-string as the input, using the
str.c_str function is necessary to form the input to
set_prop1. The function set_prop2 takes an
std::string as an input. Instead of directly using
str as an input to set_prop2,
str.c_str is used, perhaps as a copy-paste mistake. The compiler
implicitly constructs a new std::string object, which is identical to
str, by using the output of str.c_str. Constructing
a new std::string object in this case is unnecessary and inefficient.
Because this code compiles and functions correctly, this inefficient code might not be
noticed.To fix this defect, replace calls to std::string::c_str by
std::string. Consider this
code:
void set_prop1(const char* str);
void set_prop2(const std::string& str);
void foo( std::string& input){
//...
set_prop1(str.c_str()); // Necessary
//...
set_prop2(str); // Efficient
}str instead of str.c_str as input to
set_prop2 makes the code more efficient and fixes the defect.Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
EXPENSIVE_C_STR_STD_STRING_OPERATION |
| Impact: Medium |