Several common call Methods

Source: Internet
Author: User

_ Cdecl /*

_ Cdecl is short for c Declaration, indicating the default function call method of C language: All parameters are pushed to the stack from right to left. These parameters are cleared by the caller, which is called manual stack clearing. The called function does not require the caller to pass many parameters. Too many or too few parameters are transferred by the caller, and even different parameters do not produce compilation errors.

_ Stdcall is the abbreviation of standard call. It is the standard call method of c ++. All parameters are pushed to the stack from right to left. If they are call class members, the last inbound pointer is the this pointer. The parameters in the stack are cleared after the called function returns. The command is retn X, and X indicates the number of bytes occupied by the parameter, the CPU automatically pops up X bytes of stack space after ret. It is called automatic stack clearing. During compilation, the function must determine the number of parameters, and the caller must strictly control the generation of parameters. There must be no more or fewer parameters. Otherwise, an error will occur after the return.

Pascal is a function call method in PASCAL language and can also be used in C/C ++. The parameter pressure stack sequence is different from the previous two. The method of clearing the stack when the return result is missing...

_ Fastcall is a quick call method specified by the compiler. Because most function parameters are few, it is time-consuming to use stack transfer. Therefore, _ fastcall usually specifies that the first two (or several) parameters are passed by the register, and the other parameters are passed through the stack. The registers defined by programs compiled by different compilers are different. The return method is equivalent to _ stdcall.

_ Thiscall is specified to solve this pointer passing in class member calls. _ Thiscall requires that this pointer be placed in a specific register, which is determined by the compiler. VC uses ECx and Borland's c ++ compiler uses eax. The return method is equivalent to _ stdcall.

The registers involved in _ fastcall and _ thiscall are determined by the compiler. Therefore, they cannot be used as cross-compiler interfaces. Therefore, the COM object interface on Windows is defined as the _ stdcall call method.

C does not indicate that the default function is the _ cdecl method (this method can only be used in c). c ++ is the same, but the default call method can be set in the IDE environment.

A function with variable parameters must and can only use the _ cdecl method. For example, the following function:

Int printf (char * fmtstr ,...);

Int scanf (char * fmtstr ,...);

*/

Call conventions:

_ Cdecl _ fastcall and _ stdcall are call conventions (calling convention), which determine the following content: 1) the order of function parameters in the pressure stack; 2) the parameter pop-up stack is made by the caller or the caller. 3) the method for generating the function modifier name.

1. _ stdcall call Convention: The function parameters are passed from right to left through the stack. The called function clears the memory stack of the transfer parameter before returning,

2. _ cdecl is the default call Method for C and C ++ programs. Every function that calls it contains the code to clear the stack. Therefore, the size of the executable file generated is larger than that of the call to the _ stdcall function. The function uses the stack pressure mode from right to left. Note: For Variable Parameter member functions, always use the _ cdecl conversion method.

3. _ fastcall: it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters, the remaining parameters are still transmitted from the right to the left pressure stack, and the called function clears the memory stack of the transfer parameter before returning ).

4. thiscall is only applied to "C ++" member functions. This pointer is stored in the Cx register and the parameter is pressed from right to left. Thiscall is not a keyword and cannot be specified by programmers.

5. Naked call uses 1-4 call timing. If necessary, the compiler will generate code to save the ESI, EDI, EBX, and EBP registers when entering the function, when you exit the function, the code is generated to restore the content of these registers. Naked call does not generate such code. The naked call is not a type modifier, so it must be used together with _ declspec.

Call conventions can be selected through the Project Settings: setting... \ C/C ++ \ code generation item. The default status is _ cdecl.

Naming Conventions:

1. decoration name: "C" or "C ++" functions are internally (compiled and linked) identified by modifier names.

2. Agreed rules for function name modification during C Compilation:

The _ stdcall call Convention adds an underline prefix before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is _ functionname @ number. For example: function (int A, int B), whose name is: _ FUNCTION @ 8

The _ cdecl call Convention only adds an underline prefix before the output function name in the format of _ functionname.

_ Fastcall: Add a "@" symbol before the output function name, followed by a "@" symbol and the number of bytes of the parameter. The format is @ functionname @ number.

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.