Why do we allocate 12 bytes for each variable?

陌路散爱 提交于 2019-12-24 01:18:25

问题


In visual Studio 2010 Professional (x86, Windows 7):

... more
00DC1362 B9 39 00 00 00       mov         ecx,39h  
00DC1367 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
00DC136C F3 AB                rep stos    dword ptr es:[edi]  
    20:     int a = 3;
00DC136E C7 45 F8 03 00 00 00 mov         dword ptr [ebp-8],3  
    21:     int b = 10;
00DC1375 C7 45 EC 0A 00 00 00 mov         dword ptr [ebp-14h],0Ah  
    22:     int c;
    23:     c = a + b;
00DC137C 8B 45 F8             mov         eax,dword ptr [ebp-8]  
00DC137F 03 45 EC             add         eax,dword ptr [ebp-14h]  
00DC1382 89 45 E0             mov         dword ptr [ebp-20h],eax  
    24:     return 0;

Notice how the relative addressing variable A and B are not aligned by word size of 4? What is happening here?

Also, why do we skip $ebp - 8 ?

Turning off the optimization will show the ideal addressing scheme.

Can someone please explain the reason? Thanks.


The offset of each variable is 12 bytes. A -> B -> C I made a mistake. I meant why do we skip the first 8 bytes.


回答1:


You are looking at the code generated by the default Debug build setting. Particularly the /RTC option (enable run-time error checks). The filling with 0xcccccccc helps diagnose uninitialized variables, the gaps help diagnose overflow.

There isn't much point in looking at this code, you are not going to ship that. Then again, there won't be anything left of this function in the Release build.



来源:https://stackoverflow.com/questions/8033353/why-do-we-allocate-12-bytes-for-each-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!