When calling C ++ functions in C ++, virtual functions in C ++ primarily implement the polymorphism mechanism, A virtual function is implemented through a virtual function table. In C ++, you can determine that each function has a specific purpose.
In the CPU, the computer cannot know how many or what parameters are required for a function call, nor can the hardware save these parameters. That is to say, the computer does not know how to pass parameters to this function. The operation of passing parameters must be coordinated by the C ++ function caller and the function itself. Therefore, the computer provides a data structure called Stack to support parameter transmission.
Stack is an advanced post-release data structure with a storage zone and a stack top pointer. Stack top pointer pointing to the first available data item in the stack is called Stack top ). You can add data to the stack at the top of the stack. This operation is called the Push operation. After the stack is pressed, the top of the stack automatically becomes the position of the newly added data item, the stack top pointer is also modified. You can also remove the top stack from the stack, which is called pop. After the stack pops up, an element at the top of the stack changes to the top of the stack, and the top pointer of the stack changes accordingly.
When a C ++ function is called, the caller pushes the parameter to the stack in sequence and then calls the function. After the function is called, the caller obtains data in the stack and calculates the data. After function compute is completed, the caller or the function modifies the stack to restore the original stack. There are two important issues in parameter passing: when there are more than one parameter, in what order will the parameter be pushed into the stack function call, who will recover the original stack?
In advanced languages, the two problems are described through the function call convention. Common call conventions are often referred to as pascal call conventions in stdcall, because pascal is a very common language for teaching and programming, and its syntax is rigorous, the C ++ function call Convention used is stdcall. In Microsoft C ++ C/C ++ compilers, PASCAL macro is often used to declare this call Convention. Similar macros include WINAPI and CALLBACK.
The syntax declared by the stdcall call convention is (the previous function is used as an example): The call Convention of nt _ stdcall function (int a, int B) stdcall means: 1) the parameter is pushed to the stack from the right to the left. 2) The function modifies the stack. 3) the function name is automatically followed by a leading underline followed by a @ symbol, followed by the parameter size.
- Develop C ++ function parameters faster and better
- How to overload C ++ Functions
- Notes for handling C ++ static members
- How to construct C ++ virtual base class Constructor
- Easily solve C ++ exceptions
The preceding function is used as an example. parameter B is first pushed to the stack, and parameter a is followed. The function call function (1, 2) is translated into an assembly language: push 2 the second parameter to push 1 the first parameter to call the call function of the stack. Note that cs: eip is automatically put into the stack at this time, and the function itself can be translated.
- Develop C ++ function parameters faster and better
- How to overload C ++ Functions
- Notes for handling C ++ static members
- How to construct C ++ virtual base class Constructor
- Easily solve C ++ exceptions
Push ebp to save the ebp register, which will be used to save the top pointer of the stack. mov ebp can be restored when the function exits. esp saves the stack pointer mov eax, [ebp + 8 H] ebp is stored in sequence with ebp, cs: eip, a, B, ebp + 8 pointing to aadd eax, [ebp + 0CH] bmov esp is saved at ebp + 12 in the stack, and ebp restores esppop ebpret 8
Note that different compilers insert their own compilation code to provide compilation versatility, but the general code is as follows. The esp is reserved to ebp at the beginning of the function, and the restoration at the end of the function is a common method used by the compiler.
From the perspective of C ++ function calls, 2 and 1 are pushed into the stack in turn, and in the function, the parameters are accessed through the offset relative to ebp (that is, the stack pointer when the function is just introduced. After the function is completed, ret 8 indicates that the 8-byte stack is cleared and the function restores the stack.
The modifier is added because the caller does not understand the meaning of WINAPI, and the above Code will inevitably cause the stack to be destroyed. The checkesp function inserted by MFC during compilation will tell you, the stack is damaged. If the defined and used conventions are inconsistent, the stack is damaged, causing serious problems. The following are two common problems:
- The function prototype declaration and function body definition are inconsistent.
- Different function conventions are defined during DLL Function Import.