C and C ++ Calling Convention

Source: Internet
Author: User

C and C ++ Calling Convention

Calling Convention is a relatively underlying design in computer programming. It mainly involves:

Is function parameters transferred through registers or stacks? Is the function parameter stack pressed from left to right or from right to left? Whether variable parameter functions (vararg function or variadic function) are supported ). Do I need a function prototype? Does the caller (caller) or is it called or callee to clear the stack?

There are several call conventions in C and C ++: __cdecl, _ stdcall, _ fastcall, _ thiscall, _ clrcall, _ vectorcall. The following compares several call conventions.

_ Cdecl

C Declaration Calling Convention, C declares the call Convention. It is the default call convention between C and C ++. Features:

Stack is cleared by the caller (manually cleared ). The parameters are pressed from right to left. Variable parameters are supported (the function itself does not know how many parameters it has, so it needs to be cleared by the caller ). After compilation, the function name is adapted to "_ function name ". For example, _ funcname. _ Stdcall

Standard Calling Convention, a Standard call Convention. Features:

The caller clears the stack. The parameter is from the right to the sitting pressure stack (the same as _ cdecl). If the member function of the class is called, this pointer is pushed to the end. A function prototype is required, and variable parameters are not supported. Function Name Adaptation: "_ function name @ parameter byte size decimal ". For example, _ funcname @ 8. _ Fastcall

Fast Calling Convention: a quick call Convention. Use registers to solve efficiency problems. Features:

The function parameters are transmitted through registers. The leftmost DWORD (the register size is dual characters) or smaller parameters in the function are transmitted through registers. The rest is passed from right to left. Function Name Adaptation: "@ function name @ function parameter byte size decimal ". The return method is the same as _ stdcall. _ Thiscall

It is mainly used to solve the this pointer problem and use registers to pass this pointer. The return method is the same as _ stdcall.

_ Clrcall

_ Clrcall is in C ++. Net.

_ Vectorcall

It is required to pass parameters in registers as much as possible. The function name is adapted to "@ function name @ digit number in decimal format ". This is a standard added by Microsoft.

Summary

Except for _ cdecl (and _ clrcall), the stack is cleared by the caller.

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.