C and C + + calling convention

Source: Internet
Author: User
Tags function prototype variadic

The calling convention (calling convention) is a relatively low-level design in computer programming, which mainly involves:

    • Is the function parameter passed through the register or the stack?
    • is the function parameter left-to-right or right-to-left stack?
    • Whether variadic functions are supported (vararg function or variadic).
    • Do you need a function prototype?
    • Does the caller (caller) or the callee (called or callee) clean up the stack?

There are several calling conventions in C and C + +: __cdecl, __stdcall, __fastcall, __thiscall, __clrcall, __vectorcall. Below are some of the calling conventions.

__cdecl

C Declaration Calling Convention,c declares the calling convention. It is the default calling convention for C and C + +. Characteristics:

    • The stack is purged by the caller (manually cleared).
    • Parameters are stacked from right to left.
    • Variable parameters are supported (the function itself does not know how many parameters it has, so the caller needs to clear it).
    • The post-compilation function name is adapted to: "_ Function name". such as _funcname.
__stdcall

Standard calling convention, normal calling convention. Characteristics:

    • The caller clears the stack.
    • Parameters are right-to-sit-down stacks (as with __cdecl), and if the member function of the class is called, it is finally pressed into the this pointer.
    • A function prototype is required and the argument function is not supported.
    • Function name Adaptation: "_ Function name @ parameter byte size decimal". such as [email protected].
__fastcall

Fast calling convention, Quick call Convention. Solve efficiency problems by using registers. Characteristics:

    • The function parameter section is passed through the register, and the leftmost two DWORD in the function (the register size is a double word) or a smaller parameter is passed through the register. The remaining stack passes from right to left.
    • Function name adaptation: "@ Function name @ function parameter byte size decimal".
    • Returned in the same way as __stdcall.
__thiscall

Primarily used to resolve this pointer problem, use the register to pass the this pointer. Returned in the same way as __stdcall.

__clrcall

__clrcall is C + +. NET inside.

__vectorcall

Requires that parameters be passed in the register whenever possible. The function name is adapted to "@@ 函数 name @ parameter byte number decimal". This is the standard that Microsoft has added itself.

Summarize

In addition to __cdecl (and __clrcall), the other is the callee clears the stack.

C and C + + 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.