Why do you prefer char* instead of string, in C++?

后端 未结 12 1148
北海茫月
北海茫月 2021-02-01 08:31

I\'m a C programmer trying to write c++ code. I heard string in C++ was better than char* in terms of security, performance, etc, however sometimes it

相关标签:
12条回答
  • 2021-02-01 08:49

    It's safer to use std::string because you don't need to worry about allocating / deallocating memory for the string. The C++ std::string class is likely to use a char* array internally. However, the class will manage the allocation, reallocation, and deallocation of the internal array for you. This removes all the usual risks that come with using raw pointers, such as memory leaks, buffer overflows, etc.

    Additionally, it's also incredibly convenient. You can copy strings, append to a string, etc., without having to manually provide buffer space or use functions like strcpy/strcat. With std::string it's as simple as using the = or + operators.

    Basically, it's:

     std::string s1 = "Hello ";
     std::string s2 = s1 + "World";
    

    versus...

     const char* s1 = "Hello";
     char s2[1024]; // How much should I really even allocate here?
     strcpy(s2, s1);
     strcat(s2, " World ");
    

    Edit:

    In response to your edit regarding the use of char* in C++: Many C++ programmers will claim you should never use char* unless you're working with some API/legacy function that requires it, in which case you can use the std::string::c_str() function to convert an std::string to const char*.

    However, I would say there are some legitimate uses of C-arrays in C++. For example, if performance is absolutely critical, a small C-array on the stack may be a better solution than std::string. You may also be writing a program where you need absolute control over memory allocation/deallocation, in which case you would use char*. Also, as was pointed out in the comments section, std::string isn't guaranteed to provide you with a contiguous, writable buffer *, so you can't directly write from a file into an std::string if you need your program to be completely portable. However, in the event you need to do this, std::vector would still probably be preferable to using a raw C-array.


    * Although in C++11 this has changed so that std::string does provide you with a contiguous buffer

    0 讨论(0)
  • 2021-02-01 08:50

    Use std::string for its incredible convenience - automatic memory handling and methods / operators. With some string manipulations, most implementations will have optimizations in place (such as delayed evaluation of several subsequent manipulations - saves memory copying).

    If you need to rely on the specific char layout in memory for other optimizations, try std::vector<char> instead. If you have a non-empty vector vec, you can get a char* pointer using &vec[0] (the vector has to be nonempty).

    0 讨论(0)
  • 2021-02-01 08:52

    Short answer, I don't. The exception is when I'm using third party libraries that require them. In those cases I try to stick to std::string::c_str().

    0 讨论(0)
  • 2021-02-01 08:53

    C char * strings cannot contain '\0' characters. C++ string can handle null characters without a problem. If users enter strings containing \0 and you use C strings, your code may fail. There are also security issues associated with this.

    0 讨论(0)
  • 2021-02-01 08:55

    std::string is almost always preferred. Even for speed, it uses small array on the stack before dynamically allocating more for larger strings.

    However, char* pointers are still needed in many situations for writing strings/data into a raw buffer (e.g. network I/O), which can't be done with std::string.

    0 讨论(0)
  • 2021-02-01 08:56

    Compare and contrast the following C and C++ examples:

    strlen(infinitelengthstring)
    

    versus

    string.length()
    
    0 讨论(0)
提交回复
热议问题