function invocation Conventions in VC + +

Source: Internet
Author: User

We know that in the function call, there are several methods of invocation, mainly divided into C-type, Pascal-style. In C and C + +, the C-style call is the default, and the member function of the class is called _stdcall by default. There is a difference between the two, we use an example to illustrate the following:

1. __cdecl:c and C + + default invocation method

Example:

void input (int &m,int &n);/* equivalent to void __cdecl Input (int &m,int &n);

The following is the appropriate assembly code:

00401068 lea eax,[ebp-8] ;取[ebp-8]地址(ebp-8),存到eax
0040106B push eax ;然后压栈
0040106C lea ecx,[ebp-4] ;取[ebp-4] 地址(ebp-4),存到ecx
0040106F push ecx ;然后压栈
00401070 call @ILT+5(Input) (0040100a);然后调用Input函数
00401075 add esp,8 ;恢 复栈

From the above call to the input function process can be seen: before calling this function, first stack ebp-8, and then press stack ebp-4, and then call function input, the last input function call after the end of the use of esp+8 recovery stack. Thus, in the C language call in the default function modification _cdecl, by the keynote function for the parameter stack and restore the stack.

Look below: What is address ebp-8 and ebp-4?

In VC view, select Debug Windows, then select registers, display register variable values, and then in the memory under Debug windows, enter ebp-8 values and ebp-4 values (or directly enter ebp-8 and-4). Look at what values the two addresses actually store, which is actually the address of the variable n (ebp-8), the address of M (ebp-4), which shows the stack of arguments in the main calling function and the order is from right to left. In addition, because the argument is a reference to the corresponding variable, it is also proved that the reference is actually passed by the address of the variable (like a pointer).

Summary: The default function modification _cdecl in the C or C + + language call, and the function is used by the keynote to stack the parameters and restore the stacks, the actual parameters of the stack sequence is from right to left, and finally by the calling function for the stack recovery. Because the function management stack is used, the variable parameter function can be implemented. In addition, the name-decoration method is to add an underscore (_) before the function.

2. WINAPI (is actually pascal,callback,_stdcall)

Example:

void WINAPI Input (int &m,int &n);

Take a look at the assembly code for the corresponding call:

00401068 lea eax,[ebp-8]
0040106B push eax
0040106C lea ecx,[ebp-4]
0040106F push ecx
00401070 call @ILT+5(Input) (0040100a)

Related Article

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.