views:

606

answers:

6

There are different calling conventions available in C/C++ stdcall, extern, pascal etc. How many such calling conventions are available and what do each mean? Are there any links that describe these?

+8  A: 

Neither Standard C nor Standard C++ has such a concept - these are features of specific compilers, linkers and/or operating systems, so you should really indicate which specific technologies you are interested in.

anon
Considering the tags, I think he refered to Microsoft Visual C++
Cătălin Pitiș
Please check the edit history before making comments like this.
anon
+5  A: 

See Wikipedia article

and also Raymond Chen's series: First, Second, Third (this is mostly what you're asking), Fourth and Fifth

Rom
I believe your link to part 3 is wrong, it should be:http://blogs.msdn.com/oldnewthing/archive/2004/01/08/48616.aspx
Ben Schwehn
Yes, thanks. There was a typo in the link reference, I fixed the answer
Rom
+1  A: 

These concern what order to put parameters on the call stack, and when to use call by value and/or call by reference semantics. They are compiler specific extensions intended to simplify multilingual programming.

dmckee
A: 

Standard C++ basically has two: extern "C" and extern "C++". The latter is the default; this former used when you need to link to C code. Compilers may define other strings besides "C" and "C++". For instance, a compiler that's compatible with its Pascal sibling may define extern "Pascal".

Unfortunately, some compilers have invented keywords instead. In these cases, see the compiler documentation.

MSalters
Neither of those are calling conventions - they are linkage specifications.
anon
Well, as you yourself mentioned, ISO C++ doesn't have a notion of "calling conventions", and doesn't exactly describe what linkage specifications are, either - only what you can do with them. So it's a grey areas basically. That's why I gave `extern "Pascal"` as an example. A compiler can certainly use a different register allocation scheme for functions with that linkage specification.
MSalters
The two concepts are distibct. Linkage specification have to do with naming, calling conventions have to do with stack arrangement etc. It is possible and reasonable to use the same linkage specification with two different calling conventions, or vice versa.
anon
No, sorry, you're introducing a MSVC-centric view here. ISO definitely does NOT say that linkage specification is (just) naming. And one of the reasons that qsort() is overloaded on linkage is because on non-MSVC platforms the stack arrangements ("calling conventions") do differ between extern "C" and extern "C++".
MSalters
Dunno why this was downvoted - linkage specification is certainly the only thing in standard C++ that can change calling conventions.
Johannes Schaub - litb
@Neil, it *has* to do with calling convention. You are not allowed to do this, for example: extern "C" void f(); int main() { void(*fp)() = f(); } even though many compilers allow it, since they do not have different calling conventions for extern"C"/"C++", it can possibly change between those. in the code above, you would have to do: extern "C" typedef void(*fpec)(); int main() { fpec fp = fp(); } - linkage is part of the function type. See this bug report: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40336
Johannes Schaub - litb
@MSalters, can you point me to the ISO specification that discusses the stack arrangement differences between extern "C" and extern "C++" ?
mrduclaw
@mrduclaw: No, because neither the ISO C not the ISO C++ specification discusses the existance of the stack itself, let alone how it's used.
MSalters
@MSalters, my bad, I understood your statement: "stack arrangements ("calling conventions") do differ between extern "C" and extern "C++"." to mean that stack arrangements between extern "C" and extern "C++" differ. And I guess I further understood since you were talking about ISO standards that the difference was probably defined somewhere and probably not random.
mrduclaw
+1  A: 

fastcall is the optimized one but nobody uses it

Arabcoder