Function call conventions

Source: Internet
Author: User

(Conversion) C/C ++ function call conventions do not affect Program Logic in most cases. However, it is good to understand cross-language programming.

VCThe default call is _ cdecl.Method,Windows API use _ stdcallCall method. In the DLL export function, we recommend using the _ stdcall method to ensure consistency with the Windows API.

The call conventions are closely related to stack clearing. If you write an assembly function and call it to C/C ++, In the _ cdecl mode, the Assembly function does not need to clear the stack. In the _ stdcall mode, the assembler function must restore the stack before returning (RET.

C languages include _ cdecl, _ stdcall, _ fastcall, naked, and _ Pascal.

The C ++ languages include _ cdecl, _ stdcall, _ fastcall, naked, _ Pascal, and _ thiscall. There is one more _ thiscall method than the C language.

 

In VC, you can set the default call convention, and set the path:

Project-> properties-> Configuration properties-> C/C ++-> advanced-> call conversion.

 

The following describes the preceding six call methods in detail:

1, _ Cdecl

_ Cdecl is also called the C call convention. It is the default call Convention of C/C ++. AccordingThe function itself does not clean up the stack.The caller is responsible for this task,The returned value is in eax. Since the caller clears the stack, variable parameter functions, such as int sprintf (char * buffer, const char * format,...); are allowed ,...);.

 

2, _ Stdcall

_ Stdcall is often called the Pascal call convention. PASCAL Language is a very common language for teaching and programming, and its syntax is rigorous. Parameters are pushed to the stack from right to left,The function clears the stack. The returned value is in eax..

 

3, _ Fastcall

As the name implies, _ fastcall is fast because it transmits parameters through the CPU register. He uses ECx and EDX to transmit the first two DWORD or smaller parameters. The remaining parameters are imported into the stack from right to left. The function cleans up the stack and returns the values in eax.

 

4, Naked

Naked is a rare call convention and is generally not recommended. The compiler does not add initialization and cleanup code for such functions. More specifically, you cannot use return to return values. You can only use insert assembly to return results, this call Convention must be used together with _ declspec. For example, define a summation program, such as _ declspec (naked) int add (int A, int B );.

 

5, _ Pascal

This is the PASCAL Language's call Convention. Like _ stdcall, parameters are pushed to the stack from right to left. The function clears the stack and returns the stack in eax. This call method has been abandoned in VC. Therefore, it is recommended to use _ stdcall instead when writing VC programs.

 

6, _ Thiscall

This is a unique calling method in C ++ and is used in the call conventions of class member functions. If the parameter is determined, this pointer is stored in the ECX register, and the function cleans up the stack itself. If the parameter is not determined, this pointer rewrites the stack after all parameters enter the stack, and the caller cleans up the stack. _ Thiscall is not a keyword and cannot be used by programmers. Parameters are pushed to the stack from right to left.

Function call conventions

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.