简体   繁体   中英

Function calling conventions

I read a little bit about

__crlcall, __stdcall, __fastcall, __vectorcall and __thiscall

calling conversion. My question is, when is good to use one of them and what are the advantages (or disadvantages) of using them. Should i even use them?

Calling conventions are commonly used to have a function call adhere to an ABI .

An example with a different architecture (a gpu): when calling low-level CUDA routines (device functions) you have to adhere to their specific ABI, for instance you need to ensure that every load greater than 1 byte is performed on an address aligned to the multiple of the access size (ie if you need to access a 4-bytes integer, that address needs to be 4-bytes aligned).

You can't specify in C++ how registers are allocated / laid out and other low-level details (or perhaps you could but it would be a pain to tweak your code with compiler-specific extensions): that's where the compiler calling conventions come into play.

Regarding when and if you should use them: if you don't know what you're doing, you'd better not use them at all. Mismatching a calling convention on a x86 architecture like __stdcall and __cdecl could work anyway (just how things are passed during the function call is different, eg stack frames) but in general if you use a wrong calling convention if you're lucky you might just lose some performances (and render your code less portable).. if you're unlucky: CRASH ! Depending on the architecture the latter can become more and more likely.

TL;DR: Use them when you need them. Especially when dealing with low-level details from C or C++ code. Don't ever use them just to "make your code prettier".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM