Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report