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

How to analyze the way C++ function is called

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

Share

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

What this article shares with you is about how to analyze the way to call C++ functions. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it.

When calling C++ functions in C++ language, the function of virtual functions in C++ is mainly to implement the mechanism of polymorphism, and the virtual functions are realized through a table of virtual functions. In C++, it can be determined that each function has a specific purpose.

In CPU, there is no way for a computer to know how many and what parameters are required for a function call, and there is no hardware to save these parameters. In other words, the computer does not know how to pass parameters to this function, and the work of passing parameters must be coordinated by the C++ function caller and the function itself. To this end, the computer provides a data structure called stack to support parameter transfer.

The stack is a first-in-and-out data structure. The stack has a storage area and a stack top pointer. The pointer at the top of the stack points to * data items available in the stack (called the top of the stack). Users can add data to the stack above the top of the stack, which is called Push. After pressing the stack, the top of the stack automatically changes to the location of the newly added data item, and the pointer at the top of the stack is modified accordingly. Users can also remove the top of the stack from the stack, which is called pop. After popping up the stack, an element under the top of the stack becomes the top of the stack, and the pointer at the top of the stack is modified accordingly.

When the C++ function is called, the caller presses the parameters in turn, and then calls the function. After the function is called, it gets the data in the stack and calculates it. After the function evaluation is finished, either the caller or the function itself modifies the stack to restore the stack to its original load. In parameter passing, there are two very important issues that must be clearly explained: when the number of parameters is more than one, in what order will the parameters be pushed into the stack after the function call, and who will restore the stack to the original?

In high-level languages, these two problems are illustrated by function calling conventions. The common calling convention is stdcall, which is often called pascal calling convention, because pascal is a very common teaching computer programming language in the early days, its syntax is rigorous, and the C++ function calling convention used is stdcall. It is common to declare this calling convention with PASCAL macros in the Microsoft C++ family of CUniverse + compilers, such as WINAPI and CALLBACK.

The syntax of the stdcall calling convention declaration is (the previous function is an example): the calling convention of nt _ stdcall function (int a drawing int b) stdcall means: 1) the parameter is pushed into the stack from right to left, 2) the function itself modifies the stack 3) the function name is automatically underlined by an @ sign, followed by the size of the parameter.

Faster and better formulation of C++ function parameters

How to overload function on C++

Considerations when dealing with static members of C++

How to construct a function for C++ virtual base class

Solve C++ abnormal problems easily

Take the above function as an example, parameter b is first stacked, and then parameter a. The function call function (1Magazine 2) will be translated into assembly language: the second parameter of push 2 will be put into the stack push 1 * parameters into the stack call function call parameter, note that at this time automatically put the cs:eip into the stack and for the function itself, it can be translated as.

Push ebp saves the ebp register, which will be used to hold the top pointer of the stack. You can restore mov ebp,esp to save the stack pointer mov eax when the function exits. Before ebp points to the location in the [ebp + 8H] stack, ebp,cs:eip,a,b,ebp + 8 points to aadd eax in turn, and bmov esp,ebp recovery esppop ebpret 8 is saved at ebp + 12 in the [ebp + 0CH] stack.

Note that different compilers insert their own assembly code to provide compilation versatility, but in general this is the case. It is a common method for compilers to keep esp to ebp at the beginning of the function and restore at the end of the function.

From the point of view of the C++ function call, 2 and 1 are put on the stack by push in turn, and the parameters are accessed through the offset relative to ebp (that is, the stack pointer when the function is just entered). At the end of the function, ret 8 cleans up the 8-byte stack, and the function restores the stack itself.

Because the caller does not understand the meaning of WINAPI, the above code will inevitably cause the stack to be destroyed. The checkesp function inserted by MFC at compile time will tell you that the stack is broken. If the defined convention is inconsistent with the convention used, it will cause the stack to be broken, resulting in serious problems. Here are two common problems:

The function prototype declaration is inconsistent with the function body definition.

DLL declares different function conventions when importing functions

The above is how to analyze the way C++ functions are called. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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