简体   繁体   English

__cdecl的使用,其职责是清理堆栈

[英]use of __cdecl and whose responsibility is to clean the stack

I am confused about the stack cleaning after knowing about the __cdecl. 在了解__cdecl之后,我对堆栈清洁感到困惑。

Read that __cdecl will pop the stack arguments ( https://msdn.microsoft.com/en-us/library/zkwh89ks.aspx ) 请阅读__cdecl将弹出堆栈参数( https://msdn.microsoft.com/zh-cn/library/zkwh89ks.aspx

So do we need explicitly mention in function declaration to delete the stack arguments or is it compiler responsibility to generate the appropriate code to clean the stack 那么我们是否需要在函数声明中明确提及删除堆栈参数,还是由编译器负责生成适当的代码以清理堆栈?

Thanks in advnace 提前感谢

The compiler does the work, as long as the header other people #include to use your library correctly declares the function. 只要其他人( #include要使用您的库的#include头)正确声明该函数,编译器便会完成工作。 If you fail to provide a header with an appropriate declaration, they need to declare the function themselves with the appropriate calling convention, or it will use whatever the default argument passing convention happens to be for their project. 如果您未能为标头提供适当的声明,则他们需要使用适当的调用约定自行声明该函数,否则它将使用碰巧适用于其项目的任何默认参数传递约定。

Unless you are writing assembly, you don't need to write anything to clean up the stack. 除非您正在编写程序集,否则无需编写任何内容即可清理堆栈。 The compiler will generate the required code. 编译器将生成所需的代码。

Read that __cdecl will pop the stack arguments 读取__cdecl将弹出堆栈参数

You are misreading the documentation that you have linked to. 您误读了链接到的文档。 What it actually says is: 它的实际含义是:

Calling function pops the arguments from the stack. 调用函数从堆栈中弹出参数。

In cdecl, only the caller knows what arguments have been pushed on the stack (this is the only way that variadic parameters can be supported) so only the caller knows how to clean the stack correctly. 在cdecl中,只有调用者知道哪些参数已被压入堆栈(这是支持可变参数的唯一方法),因此只有调用者知道如何正确清理堆栈。 As long as you are not writing assembly code, and that your function is explicitly declared as __cdecl or your project's default calling convention is __cdecl , then you don't have to worry about this. 只要您不编写汇编代码,并且将函数显式声明为__cdecl或项目的默认调用约定是__cdecl ,您就不必担心这一点。 The compiler will generate the correct code. 编译器将生成正确的代码。 Personally, I don't suggest relying on the default project setting if you need to share this function across multiple projects, or with other people. 就个人而言,如果您需要在多个项目中或与其他人共享此功能,我建议不要依赖默认项目设置。 Better to declare the calling convention explicitly to be safe. 最好明确声明调用约定是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM