_stdcall _cdec Difference

Source: Internet
Author: User

(1) _stdcall call
_stdcall is the default calling method of Pascal program, the parameter is from right to left stack way, the function itself empties the stack before returning.
The WIN32 API uses the _stdcall invocation method, and this macro definition illustrates the problem:

#define WINAPI _stdcall

In c compilation mode, the _stdcall calling convention underlines the output function name followed by the "@" symbol and the number of bytes in the argument, in the form.

(2) _cdecl call
_CDECL is the default invocation method for C/s + +, which uses a right to left stack method, and the memory stack of the transfer parameters is maintained by the caller. The _CEDCL convention function can only be called by C + +, and each function that calls it contains the code that empties the stack, so the resulting executable file size will be larger than the call to the _stdcall function.

Because the parameter memory stack of the _cdecl invocation is maintained by the caller, the function of the variable length parameter can (and can only) use this calling convention. About variable-length parameters (...) in C + + , the author will elaborate on the other text.

Because Visual C + + defaults to _cdecl invocation, the user should use the _stdcall calling convention when calling DLLs in the VC.
In c compilation mode, the _cdecl calling convention underlines only the output function name, in the form of _functionname.

(3) _fastcall call
The _fastcall call is faster, passing parameters through the CPU's internal registers.

In c compilation mode, the _fastcall calling convention precedes the output function name with the "@" symbol followed by the "@" symbol and the number of bytes in the parameter, in the form of @functionname@number.


Almost every Windows API function we write is __stdcall type, why.
First, let's talk about the difference between the two:
A function call to Windows requires a stack (stack, a first-in-out storage structure). When the function
After the call is complete, the stack needs to be cleared, and here is the key to the problem, how to clear it.
If our function uses _cdecl, then the stack cleanup is done by the caller, in COM terms
Is the customer to complete. This poses a tricky problem where different compilers produce stacks differently
, then the caller can complete the cleanup work normally. The answer is no.
If you use __stdcall, the above problem is solved, the function solves the cleanup work by itself. So, in the cross
In the call to the platform, we all use __stdcall (although sometimes it appears as a WINAPI).
So why do we need _cdecl? When we encounter such a function as fprintf () its argument is variable
, indefinite length, the caller is unable to know the parameters in advance, and the removal of the event will not work properly.
, so we can only use _cdecl in this case.
Here we have a conclusion that if your program does not involve variable parameters, it is best to use __stdcal
L Key Words

Reprint: http://blog.csdn.net/dengziliang001/article/details/17448789

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.