StdCall, cdecl, Pascal difference (reprint)

Source: Internet
Author: User

Reprinted from: http://www.cnblogs.com/lidabo/archive/2012/11/21/2781484.html

StdCall, cdecl, Pascal difference

These three parameters are the passing conventions that tell the compiler parameters that the pass-through convention refers to the order in which parameters are passed (left to right or right to left) and by whom the stack pointer (caller or callee) is restored, under Win16 there are two conventions: C and PASCAL.

The C convention specifies that the parameter pass order is from right to left, that is, the rightmost parameter is the first to stack, and the caller recovers the stack pointer.

The Pascal convention is the opposite of the C Convention, which specifies that the parameters are passed from left to right, and the stack is restored by the callee.

StdCall is a mixture of C conventions and Pascal Conventions, which specifies that arguments are passed from right to left, and that the work of the recovery stack is done by the callee. Win32 only with StdCall convention, but except one special case, namely: wsprintf.

__stdcall This is a function call method. The parameters of the __stdcall mode function are stacked from right to left, which is the default invocation of Pascal, typically used in the Win32 API to empty the stack on exit.

__stdcall The argument stack is in the order of C (right-to-left), but unlike the C language, it is called by the callee to remove parameters from the stack, so it's compiled file is smaller than _cdecl.

__stdcall is the default calling convention in Windows API functions, and VB, VFP, etc. also adopt this Convention.

__CDECL is the default calling method used by the C language and is maintained by the caller for the memory stack that transmits the parameters. A call that implements a mutable parameter can only be used with this method. is the default invocation parameter for MFC.

The function of the __fastcall method takes the register pass parameter, the VC compiles the function to precede the function name with the "@" prefix, after the function name adds "@" and the parameter the number of bytes.

Call Convention pressure parameters into the stack order parameters pop-up stack function decorated name (calling convention)
-------------------------------------------------------------------------
__cdecl right-to-left caller _function
__fastcall right-to-left callee @[email protected]
__stdcall right-to-left callee [email protected]
__pascal left-to-right callee [email protected]
-------------------------------------------------------------------------

The above several keywords can be found in the "windef.h" header file:

#define CALLBACK __stdcall

#define WINAPI __stdcall

#define WINAPIV __cdecl

#define Apientry WINAPI

#define Apiprivate __stdcall

#define PASCAL __stdcall

#define CDECL _cdecl

#ifndef cdecl#define CDECL _cdecl

#endif

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-in-one storage structure) is required for a function call to Windows. When the function 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 the terms of COM, by the client. This brings a tricky problem, the different compiler generation stack is not the same way, then the caller can be normal to complete the cleanup work? 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 (development) platform call, we all use __stdcall (although sometimes it appears as WINAPI). So why do we need _cdecl? When we encounter such a function such as fprintf () its parameters are variable, indefinite, the caller can not know the length of the parameter beforehand, the cleanup work after the event can not be carried out normally, so we can only use _cdecl.

StdCall, cdecl, Pascal difference (reprint)

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.