Calling Convention
There are multiple function calling conventions - cdecl, stdcall, fastcall, etc. The first two are the commonly used ones.
The cdecl call convention specifies that arguments to a function are pushed on the stack in reverse order they appear in the function call, followed by the return address. This ensures that the first argument of a function is pushed last on the stack, then the return address and the Stack Pointer is set to point to the next location. Thus the first argument of the function is at an easy to compute location (ESP+8 on a 32-bit machine). This allows variable argument functions (printf and scanf) to retrieve and parse the format string to determine how many more arguments to process.
The cdecl convention requires the caller to remove the parameters (of the called functions) from the stack.
stdcall convention requires the called function to removes its own parameters from the stack, and thus restricts its usage with functions accepting fixed number of parameters.
GCC allows specifying the calling convention using the __attribute__ keyword -
void function(int x, int y) __attribute__((cdecl));
Visual C++ provides the __cdecl, __stdcall, __fastcall, etc attributes -
void __cdecl function(int x, int y);
Defining & Calling
Typedef to Rescue
Use typedef to define function pointers with ease -
typedef int (*ptrToFn)(int, int);
and then declare pointers p1, p2 as -
ptrToFn p1, p2;
declare array of pointers as -
ptrToFn pArray[10];
declare function accepting a function pointer as argument -
void function(ptrToFn p);
declare function returning a function pointer -
ptrToFn function();
There are multiple function calling conventions - cdecl, stdcall, fastcall, etc. The first two are the commonly used ones.
The cdecl call convention specifies that arguments to a function are pushed on the stack in reverse order they appear in the function call, followed by the return address. This ensures that the first argument of a function is pushed last on the stack, then the return address and the Stack Pointer is set to point to the next location. Thus the first argument of the function is at an easy to compute location (ESP+8 on a 32-bit machine). This allows variable argument functions (printf and scanf) to retrieve and parse the format string to determine how many more arguments to process.
The cdecl convention requires the caller to remove the parameters (of the called functions) from the stack.
stdcall convention requires the called function to removes its own parameters from the stack, and thus restricts its usage with functions accepting fixed number of parameters.
GCC allows specifying the calling convention using the __attribute__ keyword -
void function(int x, int y) __attribute__((cdecl));
Visual C++ provides the __cdecl, __stdcall, __fastcall, etc attributes -
void __cdecl function(int x, int y);
Defining & Calling
Typedef to Rescue
Use typedef to define function pointers with ease -
typedef int (*ptrToFn)(int, int);
and then declare pointers p1, p2 as -
ptrToFn p1, p2;
declare array of pointers as -
ptrToFn pArray[10];
declare function accepting a function pointer as argument -
void function(ptrToFn p);
declare function returning a function pointer -
ptrToFn function();