This situation can only occur without name mangling (I believe), so the below code is C.
Say there is a function A defined in A.c as
void A(int x, int y){
//Do stuff
}
Now there is also a separate file B.c:
extern "C"{
void A(int x, int y, int z);
}
void B(){
A(1, 2, 3);
}
A is initially declared to have only 2 arguments yet when declared in B.c it has an extra one, and it is called with that third in B().
I know it is possible to make this situation occur, for example when linking with fortran subroutines, OR when dynamically linking.
I imagine it is unsafe to pass an extra argument to a function, can anyone explain what is happening in memory when a function is called and arguments are passed to it? And, therefore, how safe it is to pass this “extra” argument that is neither used nor wanted.
Is it possible that the extra argument overwrites a space in memory that is used within the function? Or does the function call to A allocate space in memory for the arguments then tell A where the beginning of the argument memory block is, A reads out the first two arguments and ignores the last, making it completely safe?
Any information on the function would be greatly enlightening, thanks.
Linkage is implementation-defined, so there is no way to say definitely.
That said, other features of C (notably vardic parameters) force an implementation that would usually allow it.
For example, I don’t know of any implementation that would fail if you wrote:
It would, however, merely print “1”.
Many people here are bringing up
cdecl,pascaland__stdcallcalling conventions. However, none of those are part of the Standard and are all features of certain implementions,. which bring us back to my first sentence.