Calling conventions

Started by
3 comments, last by emilbbb 19 years, 10 months ago
What does it mean when a function has the calling convention __cdecl, __stdcall or __fastcall?
Advertisement
searching on http://msdn.microsoft.com gives

Calling Conventions

and

Argument Passing and Naming Conventions
It changes the way the parameters are passed to the function.

The __cdecl (usualy, the default one) passes parameters from right to left using stack (first the calee pushes the last parameter, then the last - 1 etc.) and the parameters are left on the stack when the called function ends (so the calee must clean them up).

The __stdcall (mostly used by the win32api calls) passes parameters from right to left using stack (like __cdecl), but the called function pops the parameters from the stack.

The __fastacall passes first 2 dword (4bytes) sized in ecx and edx registers. The rest is passed right to left.

Oxyd
A calling convention is a set of rules that dictates how arguments are passed to a function and how a value is returned. They vary by compiler and processor. Which one you choose is usually dependent on software you're developing for.

As an example, the Windows API uses __stdcall (abstracted by the WINAPI macro), and it expects callback functions to be __stdcall as well. This means that if you give the Windows API a pointer to a __cdecl function when it expected __stdcall, your program will likely crash.

Generally, you don't have to worry about calling conventions, and when you do, documentation should explain which one you need. Beyond that, you may choose to use a particular calling convention for functions within your own software, but there's rarely a need.

If you want a more specific answer, your compiler's documentation should explain the exact implementation of the calling conventions it supports. I suspect you're talking about Visual C++, so look in MSDN.
Thanks :)

This topic is closed to new replies.

Advertisement