Some pen questions on the internet

Source: Internet
Author: User
Tags true true

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 ()

    1. False false
    2. True false False
    3. True True True
    4. 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

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.