一個關於不同編譯器對operator++和operator+=的處理方式的問題,編譯器operator

來源:互聯網
上載者:User

一個關於不同編譯器對operator++和operator+=的處理方式的問題,編譯器operator

首先,上代碼:

#include <stdio.h>#include <iostream>using namespace std;int main(){int a = 5;printf("a++ = %d\n", a++);a = 5;printf("++a = %d\n", ++a);a = 5;printf("a += a++  =%d\n", a += a++);a = 5;printf("a += (a++)  =%d\n", a = a + (a++));a = 5;printf("a += ++a  =%d\n", a += ++a);a = 5;printf("a += (++a)  =%d\n", a += (++a));a = 5;printf("++a += a++ =%d\n", ++a += a++);a = 5;printf("(++a) += (a++) =%d\n", (++a) += (a++));return 1;}
不知道各位看官看到這兒有什麼想法,你認為它們的輸出值應該是什嗎?


不賣關子了,直接給出VS2013下的運行結果和g++ 4.8.2編譯出的運行結果:


如此截然不同的結果,那麼我們看一下彙編代碼(只截取了【printf("a += a++  =%d\n", a += a++);】部分的彙編作為樣本,具體的大家可以自己看一下):

/* vs2013 */    13: printf("a += a++  =%d\n", a += a++);011ECAC7  mov         eax,dword ptr [a]  011ECACA  add         eax,dword ptr [a]  011ECACD  mov         dword ptr [a],eax  011ECAD0  mov         ecx,dword ptr [a]  011ECAD3  mov         dword ptr [ebp-0D0h],ecx  011ECAD9  mov         edx,dword ptr [a]  011ECADC  add         edx,1  011ECADF  mov         dword ptr [a],edx  011ECAE2  mov         esi,esp  011ECAE4  mov         eax,dword ptr [ebp-0D0h]  011ECAEA  push        eax  011ECAEB  push        11F832Ch  011ECAF0  call        dword ptr ds:[11FC1E4h]  011ECAF6  add         esp,8  011ECAF9  cmp         esi,esp  011ECAFB  call        __RTC_CheckEsp (011E1631h)  


/* g++ 4.8.2 */13printf("a += a++  =%d\n", a += a++);   0x08048602 <+85>:mov    0x1c(%esp),%eax   0x08048606 <+89>:lea    0x1(%eax),%edx   0x08048609 <+92>:mov    %edx,0x1c(%esp)   0x0804860d <+96>:add    %eax,0x1c(%esp)   0x08048611 <+100>:mov    0x1c(%esp),%eax   0x08048615 <+104>:mov    %eax,0x4(%esp)   0x08048619 <+108>:movl   $0x8048814,(%esp)   0x08048620 <+115>:call   0x80484a0 <printf@plt>

我書讀得少,彙編更是自學成才,希望大家能幫忙解釋一下是神馬情況~~~,小弟多謝了!

【注】上述代碼運行平台均為32位作業系統,其中vs2013運行於Windows 8.1 企業版,g++運行於ubuntu14.04 (3.13.0-41-generic)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.