Differences between string in linux and windows

Source: Internet
Author: User


String a ("aaaa ");
String B;
B =;
Const char * p1 = a. c_str ();
Const char * p2 = B. c_str ();
If (p1 = p2)
{
Printf ("true ");
}
Else
{
Printf ("false ");
}
 
The above code outputs false in windows and true in linux.
The reason is that in linux, the write copy is used, so when the compiler assigns a value, it does not allocate memory, and a and B share the same memory. The memory is allocated only when the function B in string is changed.
Note that the string function is used. the pointer obtained by c_str () is forced to modify B, because this modification bypasses the copy at write time, And a and B share the same memory, so a also changes.
This article is from the "starting" blog

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.