_ Cdecl, _ stdcall, _ fastcall, and _ thiscall

Source: Internet
Author: User

1. _ 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, and these parameters are cleared by the caller, it is called manual stack clearing (the parameter is popped up by the caller ). The memory stack of the transfer parameter is maintained by the caller (because of this, the function that implements the variable parameter can only use this call Convention ). 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.

_ 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. After compiling a function, VC adds an underline prefix to the function name. Is the default MFC call convention.

2. _ 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.

_ Stdcall is the default calling method of the PASCAL program. It is usually used in WIN32API. After compiling a function, VC adds an underline prefix to the function name, and adds "@" and the number of bytes of the parameter to the function name.

3. _ fastcall: The call convention is "person" as its name. Its main feature is fast because it transmits parameters through registers (in fact, it uses ECx and EDX to transmit the first two DWORD or smaller parameters (or several), and the remaining parameters are still transmitted from the right to the left pressure stack, the called function clears the memory stack of the transfer parameter before returning). In terms of the function name modification conventions, it is different from the previous two. After compiling the function, VC adds the "@" prefix to the function name, and adds "@" and Parameter bytes to the function name.

4. _ thiscall is only applicable 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, so the Buen is specified by the programmer. _ Thiscall is specified for Class Members to call the this pointer for passing. _ 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.

5. the 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.

The keywords _ stdcall, _ cdecl, and _ fastcall can be directly added before the function to be output, or in the setting environment... select the C/C ++ code generation item. When the keywords added before the output function are different from those selected in the compiling environment, the keywords directly added before the output function are valid. Their corresponding command line parameters are/GZ,/GD, And/GR. The default status is/GD, that is, _ cdecl.

To fully imitate Pascal's call convention, you must first use the _ stdcall call Convention. As for the function name Modification Convention, you can use other methods to imitate it. Another thing worth mentioning is winapi macro, windows. h supports this macro. It can translate the output function into appropriate call conventions. In Win32, it is defined as _ stdcall. You can use the winapi macro to create your own APIs.

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.

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.