C + + string NULL terminal

Source: Internet
Author: User

In C + +, the string ending with ' s ' is a mandatory requirement, or, if the request is met

A character array can be called a string. Otherwise, all of the results you have done are undefined !

C standard library string.h all functions relating to strings have an attribute, and for input strings, it is implied that the

, otherwise undefined behavior, such as strlen, depends on the implementation:

int len = 0;

while (*s++) len++, for the output string (one is to return a char* pointer), but also to ensure that the end of adding a '

Originally, if the string is completely as a whole (like std::string), there is no need to worry about this problem, but C + + inside

The string is also in the form of a character array, so this code will appear

Char a[] = {' A ', ' B ', ' C ', ' d '};

size_t len = strlen (a);

Can be compiled, because the type check does not go wrong, but the runtime behavior is very strange, basically wrong, the correct way to define

It should be like this char a[] = {' A ', ' B ', ' C ', ' d ', ' n '};

This pit also affects the implementation of the string in the STL, such as the data () method of the string and the C_str () method, the default return char* must be the end of

But the scary thing is that string can be disassembled and used to see the following code:

#include <iostream> #include <string.h> #include <string>using namespace Std;int main () {char a[] = {' A ' , ' B ', ' C ', ' d ', '};string s (a); cout << s.size () << endl;cout << s.length () << endl;s[4] = ' e '; c Out << s << endl;cout << s.size () << endl;cout << s.c_str () << endl;cout << S.da Ta () << Endl;return 0;}

Did I find out that the start size and length output all conform to the definition of a string in C (exclude that ' + ')

But for s[4] = ' E ' This will destroy the string structure of the behavior, the compiler is indifferent, resulting in the back s.c_str () output is garbled.

Www.cplusplus.com's description of the string class's operator[] (int) method and the at (int) method are as follows:

   char& operator[] (size_t POS); const char& operator[] (size_t pos) const;
If Pos is equal to the length of the string, the function returns a reference to the null character that follows the Las t character in the string (which should isn't be modified).
Index allows equal to length, but should not is modified! That is, if the modification is undefined behavior

      char& at (size_t POS), const char& at (size_t pos) const;
Get character in string
Returns a reference to the character at position Pos in the string.

The function automatically checks whether POS is the valid position of a character in the string (i.e., whether C6>pos is less than the length of the string), throwing an out_of_range exception if it isn't.

This function is strictly forbidden to index to length! Otherwise there will be an exception!

Summary: Strings and character arrays are not the same thing! The string class in the standard library still implements strings in the form of ordinary character arrays,

So also left the potential to destroy the string structure of the hidden danger!

C + + string NULL terminal

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.