Archived

This topic is now archived and is closed to further replies.

__stdcall, __fastcall & __cdecl

This topic is 5571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What the hell do they do????? I have looked at the help but Im not too sure what its on about. Should I be using these. I notice they are MS specific... Phill Sampson Computer Game Programmer Eutechnyx LTD http://www.eutechnyx.com

Share this post


Link to post
Share on other sites
You work for "the UK''s top games developer" and don''t know what calling conventions are? And to think that I can''t even get a job...

cdecl "creates larger executables than __stdcall, because it requires each function call to include stack cleanup code."

stdcall functions are responsible for cleaning up the stack themselves, which implies smaller code. But since the function must clear up its own stack, it must always know how many arguments it is being passed and cannot cater for variable argument lists (eg. printf() style functions).

fastcall "specifies that arguments to functions are to be passed in registers, when possible." This has the potential to make that function run faster since there''s less stack manipulation. I can''t see any downsides to this providing your compiler is intelligent enough. C++ Builder uses fastcall for everything, from what I remember.

The keywords themselves are MS specific although the concepts are fairly standard. I think most compilers support all these conventions in some form or other.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
This is one of these things where if you don''t know what they are you probably don''t care.

These attributes effect several of the low-level details concerning how functions are called, how functions clean up after themselves, and how they are named. __cdecl is the standard C calling convention. __stdcall is based on the calling convention used in many Pascal compilers. __fastcall is an optimization technique.

In __cdecl the last parameter is passed to the function first and the first last. The caller of the function does the cleanup. This is done to allow variable numbers of parameters to be passed. The exact details of which I won''t get into.

In __stdcall the first paramater is passed first and the function cleans itself up. This results in slightly smaller code.

__fastcall passed some parameters in registers rather than on the stack. Again, explaining that is more detail then I''ll get into.

-Mike

Share this post


Link to post
Share on other sites
quote:
This is one of these things where if you don''t know what they are you probably don''t care.


Yes, definitely. To elaborate, here are a few of the less-than-common reasons for using calling conventions:

1. Programming in multiple compilers and distributing a library that must provide binary compatibility of its DLL for all possible clients using different compilers.

2. Systems programming where all details of the function call are absolutely important, e.g. system calls.

3. When you have a lot of very small non-member functions that must squeeze every single ounce of performance, fastcall might work well.

4. IIRC, when you work with member function pointers of classes that use multiple inheritance across different compilers.


quote:
And to think that I can''t even get a job...


I always need someone to clean off my desk. Can''t pay you much, though.

Share this post


Link to post
Share on other sites
To embellish what others have said:

Most of the time programming in windows you''ll want to use __stdcall. In fact, if you dig through the headers looking to see what WINAPI reduces to, you''ll find that it reduces to __stdcall. Times that you''ll want to use __cdecl are when you have a function that takes a variable number of arguments - like printf and related functions. Like Anon_Mike pointed out, the last argument is pushed first making it possible to identify how many arguments are passed to the function with each call. As Kyolotan pointed out - Borland compilers default to __fastcall. You''ll have to dig deeper through the docs for which ever compiler that you use to determine what the default calling convention is. By default, I mean the calling convention used when none is specified.

In straight C, the default is __cdecl - no surprise there "C decl". My understanding is that in C++, member functions default to __stdcall - but that is likely compiler dependent. And inline functions don''t have any calling convention - unless the compiler opts not to inline the function, in which case I would assume that the default is used. If that doesn''t make sense, consider what happens to an inline function - the function code becomes part of the code of the calling function - thus there''s no function call and no convention needed.

Specifying the calling convention can be important when passing a function as an argument to another function - for example - _beginthread requires a __cdecl thread function, whereas _beginthreadex requires a __stdcall thread function. And qsort takes a __stdcall function too, iirc.

Share this post


Link to post
Share on other sites
Thanks to those who helped, sorry your bitter about not having a job, and yes we are the UKs top game developer doesnt mean I have to know about every single compiler option to work for a specialist driving game developer.

Cheers Peeps

Share this post


Link to post
Share on other sites
Yeah, well I wasn''t complaining at you, I was complaining at the industry in general

Slight amendment to what LessBread said... C++ member functions use ''thiscall'' on all compilers I''ve seen, which I believe is identical to ''stdcall'' except that the ''this'' pointer is shoved in a register, being ECX on MSVC/x86. I expect this is just for efficiency since nearly all member functions will use the this pointer so it makes sense to have it in a register.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
C++ use the same argument passing convention as C ( right to left, caller cleanup) since variable-argument functions are still available even for functions which do not have C linkage. Calling conventions are not part of the C++ language itself. The only control it gives you is to whether a function should have C linkage ( unmangled name, preprended underscore ) with extern "C".

It does not default to "__stdcall", which is only ''standard'' for Microsoft APIs (think: otherwise they wouldn''t have to state that they use the calling convention).


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
It does not default to "__stdcall", which is only ''standard'' for Microsoft APIs (think: otherwise they wouldn''t have to state that they use the calling convention).

Did anyone say otherwise?



[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
My understanding is that in C++, member functions default to __stdcall - but that is likely compiler dependent.



Yup

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites