Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Visual C++: When should you care about using calling conventions?

In C/C++ (specifically, I'm using MSVS), in what situation would one ever need to worry about specifying a calling convention for a function definition? Are they ever important? Isn't the complied capable of choosing the optimal convention when necessary (ie fastcall, etc).

Maybe my understanding is lacking, but I just do not see when their would be a case that the programmer would need to care about things like the order that the arguments are placed on the stack and so forth. I also do not see why the compiler's optimization would not be able to choose whatever scheme would work best for that particular function. Any knowledge anyone could provide me with would be great. Thanks!

like image 632
Russel Avatar asked Jan 24 '11 18:01

Russel


2 Answers

In general terms, the calling convention is important when you're integrating code that's being compiled by different compilers. For example, if you're publishing a DLL that will be used by your customers, you will want to make sure that the functions you export all have a consistent, expected calling convention.

You are correct that within a single program, the compiler can generally choose which calling convention to use for each function (and the rules are usually pretty simple).

like image 61
Greg Hewgill Avatar answered Oct 22 '22 23:10

Greg Hewgill


You do not need to care for 64-bit applicatins since there is only one calling convention.

You do need to care for 32-bit applications in the following cases:

  • You interact with 3rd party libraries and the headers for these libraries did not declare the correct calling convention.
  • You are creating a library or DLL for someone else to use. You need to decide on a calling convention so that other code would use the correct calling convention when calling your code.
like image 27
John Avatar answered Oct 23 '22 01:10

John