Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the function call mode of C and C++?

Shulou Source: shulou.com Published: 2022-06-01 09:26:19 10月06日 Update

This article mainly introduces what is the function call mode of C and C++. It is very detailed and has certain reference value. Interested friends must finish reading it.

Calling mode

There are a variety of calling conventions for the Cstroke + function.

C language:

_ _ cdecl

_ _ stdcall

_ _ fastcall

Naked

_ _ pascal

C++ has one more language than C:

_ _ thiscall

1. _ _ cdecl

The _ _ cdecl calling convention is also known as the C calling convention, which is the default calling convention of the C _ _ calling + language. The parameter enters the stack from right to left, and the function itself does not clean up the stack, which is the responsibility of the caller, and the return value is in the EAX.

Since the station is cleaned by the caller, the variable parameter function is allowed to exist.

The figure shows:

Int _ cdecl add (int a, int b) {int c = a + b; return c;} int main (void) {int x = 1, y = 2; int z = add (x, y); return 0;}

The function itself does not clean up the stack, which is the responsibility of the caller

2. _ _ stdcall

The parameter goes into the stack from right to left, the function itself cleans the stack, and the return value is in EAX.

Example:

Int _ cdecl add (int a, int b) {int c = a + b; return c;} int main (void) {int x = 1, y = 2; int z = add (x, y); return 0;}

3. _ _ fastcall

As the name implies, _ _ fastcall is fast because it passes parameters through the CPU register. He uses ECX and EDX to pass the first two words (DWORD) or smaller arguments, the remaining parameters go into the stack from right to left, the function itself cleans the stack, and the return value is in EAX.

4. Naked

Naked is a rare calling convention and is generally not recommended. The compiler will not add initialization and cleaning code to this function, what is more special is that you cannot return the value with return, you can only use insert assembly to return the result. This calling convention must be used with _ declspec. For example, define a summation program, such as: _ declspec (naked) int add (int aline int b)

5. _ _ pascal

This is the calling convention of the pascal language. Like _ stdcall, the parameters go into the stack from right to left, the function itself cleans the stack, and the return value is in EAX. This method of calling has been abandoned in VS, so it is recommended to use _ stdcall instead when writing VS programs.

6. _ _ thiscall

This is a unique calling method of C++ language, which is used for the calling convention of class member functions. If the parameters are determined, the this pointer is stored in the ECX register, and the function itself cleans the stack; if the parameters are uncertain, the this pointer enters the stack after all the parameters are put on the stack, and the caller cleans the stack. _ thiscall is not a keyword and cannot be used by programmers. Parameters enter the stack from right to left.

Example:

Class Object {private: int value; int nums;public: Object (int x = 0, int y = 0) {value = x; nums = y;} ~ Object () {} void Print () const {cout

Tags: Functions parameters conventions methods functions pointers types compilers parameter tables stacks identifiers languages users examples output codes names registers formats programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology Huawei NVidia vpn