1

Refer to link https://cplusplus.com/forum/windows/56102/

The poster of the question has declared function pointers in this way:

typedef void (__cdecl* ptr)(void);
typedef double (__cdecl* ptr2)(double, double);

What is the need to put __cdecl above? I mean even without that the code should be fine.

Also, someone responded as below:

PS I see your function pointers are declared with explicit calling convention. The functions themselves should also be delcared with (the same) explicit calling convention. This is always a good idea for DLL exports, in case the default calling convention of the DLL is different to the calling app.

I don't understand the rationale behind above suggestion. Can anyone clarify as layman OR probably with a simpler code?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Helena
  • 444
  • 2
  • 15
  • What don't you understand, exactly? Most compilers default to `__cdecl` when no calling convention is specified. However, this can be changed by the user, so it's best to always be explicit about the calling convention you want to use. Both caller and called need to agree on the convention used, or else bad things happen (stack corruption, etc) – Remy Lebeau Nov 03 '22 at 03:08
  • Not depending on the client [compile option](https://learn.microsoft.com/en-us/cpp/build/reference/gd-gr-gv-gz-calling-convention?view=msvc-170) is wise. – Hans Passant Nov 03 '22 at 03:38

0 Answers0