C + + language Learning (12)--c++ language common function calling convention

Source: Internet
Author: User

C + + language Learning (12)--c++ language common function calling convention Introduction to C + + language function calling convention

In C + + development, there is no problem with the program compiling, but the reporting function does not exist at the time of the link, or there is no error in the program compiling and linking, but a stack exception occurs whenever a function in the library is called. This occurs when the code for C and C + + is mixed, or if a third-party library (non-C + + language development) is used in a C + + program, due to the function calling convention (calling convention) and the function name adornment (decorated name) rule. The function calling convention determines the order in which function parameters are stacked, and whether the caller function or the called function is responsible for clearing parameters in the stack, and the function name decoration rules determine what name decoration the compiler uses to distinguish different functions, and if the calling convention between the functions does not match or the name decoration does not match, the above problem arises.
The function calling convention in the C + + language focuses on three issues:
A, the order of the function parameters into the stack
B. The body of the cleanup stack (the body that cleans the stack: the function itself or the person who called the function)
C, Function name reorganization
The calling convention mainly refers to the way the function is called, and the function calling convention of the C + + language mainly includes stdcall,fastcall,Pascal,Cdecl,thiscall and other conventions.
In C + +, in order to allow operator overloading and function overloading, the C + + compiler usually overrides the symbolic name of each entry point according to a rule in order to allow multiple usages of the same name (with different parameter types or different scopes) without breaking the existing C-based linker. This technique is often referred to as name mangling or name decoration (named decoration). C + + compiler vendors typically choose their own name-decorating scheme.

Second, C + + language common function calling convention 1, __stdcall

__stdcallis the abbreviation for Standardcall, which is the standard calling method of C + +. __stdcallthe rules for calling conventions are as follows:
A, all parameters from right to left into the stack, if it is called the class member, the last one into the stack is the this pointer.
B, the called function automatically cleans up the stack, the return value is EAX.
C, function decorated name Convention: VC compiles the function after the function name is preceded by an underscore prefix, after the function name with "@" and the number of bytes of the parameter.

2, __cdecl

__cdeclis the abbreviation for C Declaration (Declaration, Declaration), which represents the C language default function call method. The __cdecl calling convention rules are as follows:
A, all parameters from right to left into the stack
B, all parameters are purged by the caller, called Manual clearing. The return value is in EAX
C, function decorated name Convention: VC will compile the function after the function name is preceded by the underscore prefix?
Because the stack is cleaned by the caller, a variadic function is allowed to exist, such as int sprintf (char buffer,const char format,...).

3, __fastcall

__fastcallis a fast calling convention that transmits parameters through registers. __fastcallthe rules for calling conventions are as follows:
? A, using ECX and edx to transmit the first two double word (DWORD) or smaller parameters, the remaining parameters are still transferred from the right-to-left pressure stack?
? B. The called function cleans the memory stack of the transfer parameters before returning., the return value is in EAX
? C, function decorated name Convention: VC will compile the function after the function name with the "@" prefix, after the function name with "@" and the number of bytes of the parameter?.

4, ThisCall

ThisCall is the only function modifier that cannot be explicitly specified, thiscall can only be used for calls to C + + class member functions, and ThisCall is also the default calling convention for C + + member functions. Because member function calls also have a this pointer, special handling must be done.
The thiscall calling convention is as follows:
A, using buyers transfer parameters, parameters from right to left into the stack. If the number of arguments is determined, the this pointer is passed to the callee via ECX, and if the number of arguments is indeterminate, the this pointer is pressed onto the stack after all arguments have been stacked.
B, for the variable number of arguments, the caller cleans up the stack, otherwise it is cleaned by the function of the stack
ThisCall? is not a keyword that programmers cannot use.

5, __pascal

__pascal? Language of the calling convention, with? __stdcall ? , the parameters are stacked right-to-left, the function itself cleans up the stack, and the return value is in EAX. VC is obsolete, it is recommended to use the stdcall instead.

C + + language Learning (12)--c++ language common function calling convention

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.