Differences between string and window in linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. The same piece of code:
String str1 = "sdfsdffdf ";
String str2 = str1;
There is no difference between the two sentences, but the difference has already begun in different systems!
Print the address of the string data storage respectively:
Const char * p1 = str1.c _ str ();
Const char * p2 = str2.c _ str ();
Cout. unsetf (ios_base: dec );
Cout. setf (ios_base: hex );
Cout <(int) p1 < Cout <(int) p2 < The difference is obvious now!
The output in Window is:
12ff4c
12ff14
In linux, the output is:
2830104c
2830104c
Now let's see the difference! In linux, two different Strings actually point to the same memory, while in windows, they point to different memory! That is to say, in the window, there are two strings in the true sense. Linux is not a true string?
To verify this, add the following statement to the program in linux:
Transform (str2.begin (), str2.end (), str2.begin (), (int (*) (int) tolower); // # include
Then let's look at the address indicated by the output pointer:
Cout <(int) p1 < Cout <(int) p2 < The amazing result is:
2830104c
2830.0c
Compare the previous output and find that the address of the second string has changed. When will it be changed?
After experiments, we found that the original string template in linux was a bit fraudulent! For example, for the top two sentences, it will find that you have not performed any substantive operations on strS2, because the program just printed the address, therefore, it does not allocate space for strS2 to save strS1 strings! But it points the strS2 pointer to the address space of strS1! A little abnormal. However, if you add multiple transform functions after the program to operate strS2, you will find that the system has allocated space for strS2! I am a little depressed about this, because I wrote a C function to operate strS2, and the system did not allocate space for strS2! Only g ++ can determine the space allocation of strS2. It can only be allocated based on the C ++ operation function!
It is said that this feature in linux is designed to speed up the program running! It seems to be.
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.