Unnecessary use of std::string::c_str() or equivalent string
methods
Instead of a std::string object, a string operation uses the
C-string obtained from std::string functions including
std::string::c_str, std::string::data(),
std::string::at(), or std::string::operator[],
resulting in inefficient code
Description
This defect occurs when a string operation is performed by using a C-string pointer
obtained from string functions such as std::string::c_str,
std::string::data(), std::string::at(), and
std::string::operator[]. For instance, this checker is raised when:
A new
std::stringorstd::wstringis implicitly or explicitly constructed from the C-string obtained from a string function. This situation arises when a function expecting aconstreference to the string encounters aconst char*instead.A new copy of a string object is created explicitly from the C-string obtained from a string function. Using the copy constructor is the more efficient way of copying the string object.
Certain
std::stringmember functions are invoked by using the C-string obtained from a string function. Flagged functions includereplace,append,assign,compare, andfind. Using anstd::stringobject directly to invokestd::stringmember functions is more efficient.A user-defined function that is overloaded to accept either of the
const char*orconst std::stringarguments is invoked by using a C-string pointer. It is more efficient to invoke thestd::stringoverload of such a function. When a function is overloaded in this way, calling theconst char*overload from the body of theconst std::stringoverload by using the C-string pointer does not raise the defect.A
std::string_viewobject is constructed from a C-string obtained from astd::stringobject, either implicitly or explicitly. For example, it is inefficient to pass a C-string obtained from astd::string objectto a function that accepts astd::string_viewparameter. Calling the function using thestd::stringobject is more efficient.
Risk
It is expensive and inefficient to use the C-string output of a
std::string function when you can use an std::string
object instead. An std::string object contains the length of the string.
When you use a C-string instead of an std::string object, the constructor
determines the length of the C-string by a linear search, resulting in inefficient code.
Using the C-string 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.Fix
To fix this defect, eliminate calls to std::string functions that
produce a C-sting. Use std::string instead. Choose appropriate function
overloads when you use a string object instead of a C-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.
Examples
Result Information
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
EXPENSIVE_C_STR_STD_STRING_OPERATION |
| Impact: Medium |
Version History
Introduced in R2020bSee Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)