C ++, have you fallen into a few small traps in the string

  Projects developed in C ++ will inevitably use STL strings, which are much more convenient to use and manage than char arrays (pointers). However, we must be alert to a few small pitfalls during the handy use process to avoid bugs in our projects but we ca n’t find them To the cause.
  
  1. The problem of string assignment in the structure is
  
  directly explained by an example, what will the following example output:
  
  bubuko.com, 布布 扣
  
  #include <iostream>
  
  #include <string>
  
  #include <stdlib.h>
  
  using namespace std;
  
  struct flowRecord
  
  {
  
  string app_name;
  
  struct flowRecord * next;
  
  };
  
  int main () {
  
  flowRecord * fr = (flowRecord *) malloc (sizeof (flowRecord));
  
  fr-> app_name = "hello";
  
  out << fr-> app_name << endl;
  
  return 0;
  
  }
  
  bubuko.com, cloth button
  
  Um, of course, it's not simply output "hello", try to compile and run under g ++ under Linux, and it will show "Segmentation fault (core dumped)", why? The problem is when allocating memory to the fr pointer. Note that malloc in C is used instead of new. If you change to new and run again, you will not report an error. If you successfully output "hello", why malloc Can't it? This depends on the difference between malloc () and new (). The difference between the two is the classic interview questions that are frequently asked in programmer interviews, so I believe that ordinary programmers know that there is a very important difference between them. That is: new will call the default constructor when allocating memory, but malloc will not. The STL string needs to call the default constructor to initialize the string before assignment, such as assignment, printing and other operations, if you use malloc to allocate memory, the string default constructor will not be called to initialize the app_name character in the structure String, so it is wrong to assign it directly here, the new operator should be used. This also reminds us to use the functions in C ++ as much as possible when developing programs with C ++. Do not mix C ++ and C programming, which leads to confusion. For example, sometimes the memory allocated by new is released with free.
  
  2. The c_str () function problem The
  
  c_str () function is used to convert between string and const char *, and is often used. What do you say about the output in the following example?
  
  bubuko.com, 布布 扣
  
  #include <iostream>
  
  #include <string>
  
  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;
  
  }
  
  bubuko.com, Bubukou
  
  . The first one goes without saying, the second output is "Alexia" or "Amexia"? The answer is the latter. At first glance, the value of const char * should be a constant. How can it be changed? Ha, another classic interview question: What is the difference between const char *, char const *, char * const? An old question, const char * and char const * are equivalent, referring to pointers to character constants, that is, pointers cannot be changed but the content they point to can be changed, while char * const is opposite, referring to constant pointers, ie The pointer can be changed but the content pointed to by the pointer cannot be changed. Therefore, the content pointed to by const char * can be changed, so why did it change? This is related to the life cycle of the const char * str and the implementation of the string class. The pointer returned by c_str () of the string is managed by the string, so its lifetime is the lifetime of the string object, and the implementation of the string class is actually Encapsulates a char * pointer, and c_str () directly returns the reference of the pointer, so the change of the string object will directly affect the pointer reference returned by the executed c_str ().
  
  3. The string literal value is not the same type as the standard library string.
  
  See the following example directly:
  
  string s ("hello");
  
  cout << s.size () << endl; // OK
  
  cout << "hello". size () << endl;
  
  cout << s + "world" << endl; // OK
  
  cout << "hello" + "world" << endl; // ERROR
  
  shows that the two are very different and cannot be confused.

Guess you like

Origin www.cnblogs.com/zhenhua1618/p/12729488.html