C ++ projects will inevitably use STL strings. It is much easier to use and manage than char arrays (pointers). However, you must be cautious with several small traps during ease of use, to avoid bugs in our project, but we are too late to find the cause.
1. string Value assignment in structThe following example shows what the output will be:
#include
#include
#include
using namespace std;struct flowRecord { string app_name; struct flowRecord *next;};int main() { flowRecord *fr = (flowRecord*)malloc(sizeof(flowRecord)); fr->app_name = "hello"; cout << fr->app_name << endl; return 0;}
Well, of course it's not a simple output of "hello". In Linux, use g ++ to compile and run the command. "Segmentation fault (core dumped)", why? The problem is that when allocating memory to the fr pointer, it should be noted that the malloc in C is used instead of new. If you change to new and then run it, no error will be reported, if "hello" is successfully output, why does malloc fail? This depends on the difference between malloc () and new (). The difference between the two is the classic interview questions frequently asked by programmers during interviews, so I believe that programmers generally know that there is a very important difference between them: new will call the default constructor when allocating memory, while malloc will not. Before assigning values to strings in STL, you need to call the default constructor to initialize strings, such as assigning values and printing. If you use malloc to allocate memory, the default string constructor is not called to initialize the app_name string in the struct. Therefore, it is incorrect to assign a value directly to the string. The new operator should be used. This also prompts that when we use C ++ to develop programs, we should try to use the functions in C ++, instead of mixed programming between C ++ and C, resulting in confusion, for example, sometimes the new memory is released with free.
2. c_str () function problemsThe c_str () function is often used to convert string and const char *. In the following example, what is the output?
#include
#include
using namespace std;int main() { string s = "Alexia"; const char *str = s.c_str(); cout << str << endl; s[1] = 'm'; cout << str << endl; return 0;}
Well, the first one is needless to say, and the second output is "Alexia" or "Amexia? The answer is the latter. What should I do if the value of const char * is a constant? How can I change the value? What is the difference between const char *, char const *, and char * const? The conventional problem is that const char * is equivalent to char const *, which refers to a pointer to a character constant, that is, the pointer cannot be changed but the content it points to can be changed, char * const, on the contrary, refers to a constant pointer, that is, the content that points to a variable but cannot be changed. Therefore, the content indicated by const char * can be changed. Why is it changed? This is related to the life cycle of the const char * and the implementation of the string class. The pointer returned by the string c_str () is managed by the string, so its life cycle is the life cycle of the string object, the implementation of the string class actually encapsulates a char * pointer, while c_str () directly returns a reference to this pointer. Therefore, changes to the string object will directly affect the executed c_str () pointer Reference returned.
3. The string Literal Value and the standard library string are not of the same typeLet's look at the example below:
String s ("hello"); cout <
It can be seen that the two are very different and cannot be mixed.