The above code compiles and runs correctly. Is this practice (calling a function via pointer with too many parameters) potentially harmful, or can I expect it to work every time?
Is this practice (calling a function via pointer with too many parameters) potentially harmful.
I would say Yes and No- because the function would not be aware of the additional parameters. There would be no memory leak because the caller is responsible for parameter cleanup.
This of course makes the assumption that the parameter positions that match up are of the correct type.
The following however could be harmful;
1. Calling with wrong parameters type
IIRC, C (and hence, C++) conform to the platform standard for stack frames... so it could very well be dangerous to pass too many arguments (even of the correct types). Don't do it.
A pointer to a function can be explicitly converted to a pointer to a function of a different type. The effect of calling a function through a pointer to a function type (8.3.5) that is not the same as the type used in the definition of the function is undefined. Except that converting an rvalue of type “pointer to T1” to the type “pointer to T2” (where T1 and T2 are function types) and back to its original type yields the original pointer value, the result of such a pointer conversion is unspecified. [ Note: see also 4.10 for more details of pointer conversions. — end note ]
No, it is about function type pointers -- and the number of parameters a function takes is part of its type. That's why you need to declare prototypes for functions in different modules.
There is also the symantics to consider. If you're passing more parameters, even though the types match, are you passing in something that is meaningful?
Windows supports the Pascal calling convention, now called stdcall, where the called function pops the stack. In that context, it will be technically wrong to attempt the call.