C and C ++ storage modes

Source: Internet
Author: User

Test environment: WINXP tc2.0 VC ++ 6.0

 

 

# Include <stdio. h>

Int Main ( Int Argc, Char * Argv [])
{

Int I = Zero X 1234 ;
Int J = Zero X 5678 ;
Printf ( " I = % x \ n " , I );
Printf ( " J = % x \ n " , J );
Printf ( " Main = % x " , Main );
Scanf (& I, & J );
Return 0 ;
}

 

Corresponding assemblyCode:

 

Var_ I = byte PTR- 4
Var_j = word PTR- 2
Argc = word PTR 4
Argv = DWORD PTR 6
Envp = dword ptr 0ah

PushBP
MoVBP, SP
SubSP,4
MoVWord PTR [bp + var_ I], 1234 H
MoV[Bp + var_j], 5678 H
PushWord PTR [bp + var_ I]
MoVAx, 194 H

From the assembly code above, we can see that the address of the variables first defined in C in the stack is relatively low, and the address in the variable stack defined later is high.

 

In vc6.0

 

Int A = Zero X 12345678 ;
Int B = Zero X 1234 ;

 

 

Assembly Code

 

6 : Int I = 0x1234 ;
00401028 MoV Dword ptr [EBP- 4 ], 1234 H
7 : Int J = 0x5678 ;
0040102f MoV Dword ptr [EBP- 8 ], 5678 H

 

MoV [EBP + var_24], 12345678 H ; A
MoV [EBP + var_28], 1234 H ; B

Conclusion: In C ++ and C in vc6.0, the requested variable address is high, and the requested address is low. This is similar to applying for a first entry to the stack, and then applying for a later entry to the stack.

This seems to be related to the compiler.

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.