简体   繁体   中英

use of __cdecl and whose responsibility is to clean the stack

I am confused about the stack cleaning after knowing about the __cdecl.

Read that __cdecl will pop the stack arguments ( https://msdn.microsoft.com/en-us/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. 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

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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