Quantcast
Channel: User Marco A. - Stack Overflow
Viewing all articles
Browse latest Browse all 45

Answer by Marco A. for Function calling conventions

$
0
0

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 (i.e. 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, e.g. 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".


Viewing all articles
Browse latest Browse all 45

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>