C ++ basic review-string address comparison

Source: Internet
Author: User
Char str1 [] = "ABC"; char str2 [] = "ABC"; const char str3 [] = "ABC"; const char str4 [] = "ABC "; const char * str5 = "ABC"; const char * str6 = "ABC"; cout <boolalpha <(str1 = str2) <Endl; // What is output? Cout <boolalpha <(str3 = str4) <Endl; // What is output? Cout <boolalpha <(str5 = str6) <Endl; // What is output?

What is the output result?

 
Falsefalsetrue

Why? Disassembly

Char str1 [] = "ABC"; 00cd163e mov eax, dword ptr [String "ABC" (0cd9a40h)] // convert the ABC string (static address) 00cd1643 mov dword ptr [str1], eax // str1 address 0x0018fd00 char str2 [] = "ABC"; 00cd1646 mov eax, dword ptr [String "ABC" (0cd9a40h)] 00cd164b mov dword ptr [str2], eax // str2 address 0x0018fcf4 const char str3 [] = "ABC"; 00cd164e mov eax, dword ptr [String "ABC" (0cd9a40h)] 00cd1653 mov dword ptr [str3], eax // str3 address 0x0018fce 8 const char str4 [] = "ABC"; 00cd1656 mov eax, dword ptr [String "ABC" (0cd9a40h)] 00cd165b mov dword ptr [str4], eax // str4 address 0x0018fsp_const char * str5 = "ABC"; 00cd165e mov dword ptr [str5], offset string "ABC" (0cd9a40h) // str5 points to 0cd9a40hconst char * str6 = "ABC"; 00cd1665 mov dword ptr [str6], offset string "ABC" (0cd9a40h) // str6 points to 0cd9a40hcout <boolalpha <(str1 = str2) <Endl; // What is output? 00cd166c mov ESI, esp 00cd166e mov eax, dword ptr [_ imp_std: Endl (0cdd31ch)] 00cd1673 push eax 00cd1674 Lea ECx, [str1] 00cd1677 Lea edX, [str2] 00cd167a CMP ECx, EDX 1_sete Al 1_mov EDI, esp 00cd1681 movzx ECx, Al 00cd1684 push ECx 00cd1685 mov EBX, esp 00cd1687 push offset STD: boolalpha (0cd1113h) 00cd168c mov ECx, dword ptr [_ imp_std: cout (0cdd318h)] 00cd1692 call dword ptr [_ imp_std:: Basic_ostream <char, STD: char_traits <char >:: operator <(0cdd30ch)] 00cd1698 cmp ebx, esp 00cd169a call @ ILT + 505 (_ rtc_checkesp) (0cdfeh) 00cd169f mov ECx, eax 00cd16a1 call dword ptr [_ imp_std: basic_ostream <char, STD: char_traits <char >:: operator <(0cdd308h)] 00cd16a7 cmp edi, esp 00cd16a9 call @ ILT + 505 (_ rtc_checkesp) (011feh) 00cd16ae mov ECx, eax 00cd16b0 call dword ptr [_ imp_std: basic_o Stream <char, STD: char_traits <char >>:: operator <(0cdd310h)] 00cd16b6 cmp esi, esp 00cd16b8 call @ ILT + 505 (_ rtc_checkesp) (0cd11feh) cout <boolalpha <(str3 = str4) <Endl; // What is output? 00cd16bd mov ESI, esp 00cd16bf mov eax, dword ptr [_ imp_std: Endl (0cdd31ch)] 00cd16c4 push eax 00cd16c5 Lea ECx, [str3] 00cd16c8 Lea edX, [str4] 00cd16cb CMP ECx, EDX 00cd16cd sete Al 1_mov EDI, esp 00cd16d2 movzx ECx, Al 00cd16d5 push ECx 00cd16d6 mov EBX, esp pull offset STD: boolalpha (0cd1113h) 00cd16dd mov ECx, dword ptr [_ imp_std: cout (0cdd318h)] 00cd16e3 call dword ptr [_ imp_std:: Basic_ostream <char, STD: char_traits <char >:: operator <(0cdd30ch)] 00cd16e9 cmp ebx, esp 00cd16eb call @ ILT + 505 (_ rtc_checkesp) (0cdfeh) 00cd16f0 mov ECx, eax 00cd16f2 call dword ptr [_ imp_std: basic_ostream <char, STD: char_traits <char >:: operator <(0cdd308h)] 00cd16f8 cmp edi, esp 00cd16fa call @ ILT + 505 (_ rtc_checkesp) (0cdfeh) 00cd16ff mov ECx, eax 00cd1701 call dword ptr [_ imp_std: basic_o Stream <char, STD: char_traits <char >:: operator <(0cdd310h)] 00cd1707 cmp esi, esp 00cd1709 call @ ILT + 505 (_ rtc_checkesp) (0cd11feh) cout <boolalpha <(str5 = str6) <Endl; // What is output? 00cd170e mov ESI, esp 00cd1710 mov eax, dword ptr [_ imp_std: Endl (0cdd31ch)] 00cd1715 push eax 00cd1716 mov ECx, dword ptr [str5] 00cd1719 CMP ECx, dword ptr [str6] 00cd171c sete DL 1_mov EDI, esp 00cd1721 movzx eax, DL 00cd1724 push eax 1_mov EBX, esp 00cd1727 push offset STD: boolalpha (0cd13h) 00cd172c ECx, dword ptr [_ imp_std: cout (0cdd318h)] 00cd1732 call dword ptr [_ imp_std: basic_ostream <char, STD: char_traits <char> :: operator <(operator)] 00cd1738 cmp ebx, esp 00cd173a call @ ILT + 505 (_ rtc_checkesp) (0cdfeh) 00cd173f mov ECx, eax 00cd1741 call dword ptr [_ imp_std:: basic_ostream <char, STD: char_traits <char >:: operator <(0cdd308h)] 00cd1747 cmp edi, esp 00cd1749 call @ ILT + 505 (_ rtc_checkesp) (0cdfeh) 00cd174e mov ECx, eax 00cd1750 call dword ptr [_ imp_std: basic_ostream <char, STD: char_traits <char >:: operator <(0cdd310h)] 00cd1756 cmp esi, esp 00cd1758 call @ ILT + 505 (_ rtc_checkesp) (0cd11feh)

 

So here

 

Str1 (0x0018fd00 )! = Str2 (0x0018fcf4)

 

Str3 (0x0018fce8 )! = Str4 (0x0018fcdc)

Str5 (0x0cd9a40h) = str6 (0x0cd9a40h)

Result:

False

False

True

 

 

 

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.