C++基础回顾-字符串地址比较

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; // 输出什么?
cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?
cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?

输出结果是什么?

false
false
true

why?反汇编看看

	char str1[]       = "abc";
00CD163E  mov         eax,dword ptr [string "abc" (0CD9A40h)]  //将abc字符串(静态地址)
00CD1643  mov         dword ptr [str1],eax //str1地址 0x0018fd00 
	char str2[]       = "abc";
00CD1646  mov         eax,dword ptr [string "abc" (0CD9A40h)]  
00CD164B  mov         dword ptr [str2],eax  //str2地址 0x0018fcf4 
	const char str3[] = "abc"; 
00CD164E  mov         eax,dword ptr [string "abc" (0CD9A40h)]  
00CD1653  mov         dword ptr [str3],eax  //str3地址 0x0018fce8
	const char str4[] = "abc"; 
00CD1656  mov         eax,dword ptr [string "abc" (0CD9A40h)]  
00CD165B  mov         dword ptr [str4],eax  //str4地址 0x0018fcdc
	const char* str5  = "abc";
00CD165E  mov         dword ptr [str5],offset string "abc" (0CD9A40h)  //str5指向0CD9A40h
	const char* str6  = "abc";
00CD1665  mov         dword ptr [str6],offset string "abc" (0CD9A40h)  //str6指向0CD9A40h
	cout << boolalpha << ( str1==str2 ) << endl; // 输出什么?
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  
00CD167C  sete        al  
00CD167F  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) (0CD11FEh)  
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) (0CD11FEh)  
00CD16AE  mov         ecx,eax  
00CD16B0  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0CDD310h)]  
00CD16B6  cmp         esi,esp  
00CD16B8  call        @ILT+505(__RTC_CheckEsp) (0CD11FEh)  
	cout << boolalpha << ( str3==str4 ) << endl; // 输出什么?
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  
00CD16D0  mov         edi,esp  
00CD16D2  movzx       ecx,al  
00CD16D5  push        ecx  
00CD16D6  mov         ebx,esp  
00CD16D8  push        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) (0CD11FEh)  
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) (0CD11FEh)  
00CD16FF  mov         ecx,eax  
00CD1701  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0CDD310h)]  
00CD1707  cmp         esi,esp  
00CD1709  call        @ILT+505(__RTC_CheckEsp) (0CD11FEh)  
	cout << boolalpha << ( str5==str6 ) << endl; // 输出什么?
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  
00CD171F  mov         edi,esp  
00CD1721  movzx       eax,dl  
00CD1724  push        eax  
00CD1725  mov         ebx,esp  
00CD1727  push        offset std::boolalpha (0CD1113h)  
00CD172C  mov         ecx,dword ptr [__imp_std::cout (0CDD318h)]  
00CD1732  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0CDD30Ch)]  
00CD1738  cmp         ebx,esp  
00CD173A  call        @ILT+505(__RTC_CheckEsp) (0CD11FEh)  
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) (0CD11FEh)  
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)  

 

所以这里

 

str1(0x0018fd00)!=str2(0x0018fcf4)

 

str3(0x0018fce8)!=str4(0x0018fcdc)

str5(0x0CD9A40h)==str6(0x0CD9A40h)

结果为

false

false

true  

 

 

 

  

  

posted @ 2012-05-06 15:45  Yarkin  阅读(1033)  评论(0编辑  收藏  举报