Pointer to const char vs char array vs std::string

前端 未结 7 2019
不思量自难忘°
不思量自难忘° 2020-12-16 20:48

Here I\'ve two lines of code

const char * s1 = \"test\";
char s2 [] = \"test\";

Both lines of code have the same behavior, so I cannot see

相关标签:
7条回答
  • 2020-12-16 21:14
    const char * s1 = "test";
    char s2 [] = "test";
    

    These two aren't identical. s1 is immutable: it points to constant memory. Modifying string literals is undefined behaviour.

    And yes, in C++ you should prefer std::string.

    0 讨论(0)
  • 2020-12-16 21:19

    These two do not have the same behavior. s1 is a simple pointer which is initialized to point to some (usually read-only) area of the memory. s2, on the other hand, defines a local array of size 5, and fills it with a copy of this string.

    Formally, you are not allowed to modify s1, that is, do something like s1[0] = 'a'. In particular, under weird circumstances, it could cause all other "test"s in your program to become "aest", because they all share the same memory. This is the reason modern compilers yell when you write

    char* s = "test";
    

    On the other hand, modifying s2 is allowed, since it is a local copy.

    In other words, in the following example,

    const char* s1 = "test";
    const char* s2 = "test";
    char s3[] = "test";
    char s4[] = "test";
    

    s1 and s2 may very well point to the same address in memory, while s3 and s4 are two different copies of the same string, and reside in different areas of memory.

    If you're writing C++, use std::string unless you absolutely need an array of characters. If you need a modifiable array of characters, use char s[]. If you only need an immutable string, use const char*.

    0 讨论(0)
  • 2020-12-16 21:22

    The only difference between the two that you should care about is this:

    Which one is your project already using?

    0 讨论(0)
  • 2020-12-16 21:25

    which one to be used depends upon your requirement. Pointer offers you more flexiblity. and in some cases vulerability. Strings are a safe option and they provide Iterator support.

    0 讨论(0)
  • 2020-12-16 21:26

    The first one is constant, the second isn't. std::string is a class type and implements many useful functions and methods for string manipulation, making it much easier and user-friendly. The c-style 'strings' with char pointers are difficult to control, manipulate and often cause errors, but don't have the overhead the std::string has. Generally it's better to stick to the std::strings cause they're easier to maintain.

    0 讨论(0)
  • 2020-12-16 21:27
    POINTERS
    --------
    
    char const* s1 = "test";  // pointer to string literal - do not modify!
    
    char* s1       = "test";  // pointer to string literal - do not modify!
                              //   (conversion to non-const deprecated in C++03 and
                              //       disallowed in C++11)
    
    ARRAYS
    ------
    
    char s1[5]     = "test";  // mutable character array copied from string literal
                              //    - do what you like with it!
    
    char s1[]      = "test";  // as above, but with size deduced from initialisation
    
    
    
    CLASS-TYPE OBJECTS
    ------------------
    
    std::string s1 = "test";  // C++ string object with data copied from string
                              //    literal - almost always what you *really* want
    
    0 讨论(0)
提交回复
热议问题