1. (http://group.jobbole.com/13838/)
Sogou 2016 research and development engineer Pen test
1 intMainintargcChar*argv[])2 {3 stringA="Hello World";4 stringb=A;5 if(A.C_STR () = =b.c_str ())6 {7cout<<"true"<<Endl;8 }9 Elsecout<<"false"<<Endl;Ten stringC=b; OneC=""; A if(A.C_STR () = =b.c_str ()) - { -cout<<"true"<<Endl; the } - Elsecout<<"false"<<Endl; -A=""; - if(A.C_STR () = =b.c_str ()) + { -cout<<"true"<<Endl; + } A Elsecout<<"false"<<Endl; at return 0; -}
The correct result about the code output is ()
- False false
- True false False
- True True True
- True True False
Run the result on my WINDOWS10+TDM gcc 5.x is all false
As you can see from the debug results in GdB, C_STR returns a character pointer, which means that every string object that applies the function results in a character array, the first address pointer of the array is returned, and according to subsequent debugs, such as a= "", The position of the first address of this array is fixed, that is, the first time the pointer returns to the A.C_STR is
(GDB) P A.c_str ()
$ = 0x24fe20 "Hello World"
After A= ""
(GDB) P A.c_str ()
$6 = 0x24fe20 ""
The address has not changed.
Therefore, all the c_str results of A and b above are not equal in comparison.
In addition to the knowledge points in C + + primer 5, the use of B=a and B (a) effect is similar, is the copy initialization rather than direct initialization, so two objects are not the same, but for the overloaded = =, is based on the same characters are returned true.
Some pen questions on the internet