_stdcall,_cdecl differences

Source: Internet
Author: User

(1) _stdcall call
_stdcall is the default invocation of the Pascal program, and the parameters are stacked in a right-to-left way, and the function itself empties the stack before returning.
The WIN32 API is _stdcall called, and the macro definition illustrates the problem:

#define WINAPI _stdcall

By C compilation, the _stdcall calling convention is preceded by an underscore in the output function name, followed by the "@" symbol and the number of bytes of the parameter, as the form.

(2) _cdecl call
_CDECL is the default call method for C/s + +, which uses a right-to-left compression stack, and the memory stack that transmits the 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 is larger than the call to the _stdcall function.

Since 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 the variable length parameter (...) in C + + , I will elaborate on the other article.

Because Visual C + + defaults to _cdecl invocation, users should use the _stdcall calling convention when calling DLLs in VCs.
By C compilation, the _cdecl calling convention is underlined only in front of the output function name, as in the form of _functionname.

(3) _fastcall call
The _fastcall call is faster, and it passes parameters through the internal CPU registers.

By C compilation, the _fastcall calling convention precedes the output function name with the "@" symbol followed by the "@" symbol and the number of bytes of the parameter, shaped like @[email protected].

Almost every Windows API function we write is __stdcall type, why??
First, let's talk about the difference between the two:
A stack (a stack, a first-out storage structure) is required for a function call to Windows. When the function
After the call is completed, the stack needs to be cleared, here is the key to the problem, how to clear??
If our function uses _cdecl, then the cleanup of the stack is done by the caller, in COM terms
Is the customer to complete. This poses a tricky problem, with different compilers generating stacks differently
, will the caller be able to complete the cleanup work properly? The answer is no.
If you use __stdcall, the above problem is solved, the function solves the cleanup work by itself. Therefore, in cross-
(development) Platform, we all use __stdcall (though sometimes appearing as WINAPI).
So why do we need _cdecl? When we encounter such a function as fprintf () its arguments are mutable
, the caller cannot know the length of the parameter beforehand, and the cleanup work will not proceed normally.
, 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 Keywords

_stdcall,_cdecl differences

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.