Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we allocate 12 bytes for each variable?

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.

like image 401
CppLearner Avatar asked Nov 27 '25 13:11

CppLearner


1 Answers

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

There isn't much point in looking at this code, you are not going to ship that. It is purely a Debug build artifact, only there to help you get the bugs out of the code. None of it remains in the Release build.

like image 53
Hans Passant Avatar answered Nov 30 '25 06:11

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!