Recently I've got a strange bug/feature. Developing C function using C Matrix API in MATLAB I have done a mistake by passing more arguments than was declared by function, and the compiler didn't raise an error about it.
A small example of a problem using Matlab C Matrix API - mex_with_bug.c:
#include "mex.h"
int function_which_declares_one_input(int a) {
return a;
}
void mexFunction(int hlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
// expected usage
function_which_declares_one_input(1);
// totally not expected use cases
function_which_declares_one_input(1, 2);
function_which_declares_one_input(1, 2, 3);
function_which_declares_one_input(1, 2, 3, 'a');
}
Compilation command in Matlab:
mex -v COMPFLAGS="$COMPFLAGS -Wall" mex_with_bug.c
Only gives a warning and no errors:
...
mex_with_bug.c(13): warning C4020: "function_which_declares_one_input": too many real parameters
mex_with_bug.c(14): warning C4020: "function_which_declares_one_input": too many real parameters
mex_with_bug.c(15): warning C4020: "function_which_declares_one_input": too many real parameters
...
And calling function mex_with_bug
works just fine:
>> mex_with_bug()
>>
Why no error was raised? And why this 'feature' is needed by C?