c - What happens if I cast a function pointer, changing the number of parameters -


I just started to wrap my head around the function pointers in C. To understand how the function of pointers works, I wrote the following program: It basically creates a function pointer in a function that takes a parameter, put it on the function pointer with three parameters and the function call Supports three parameters. I was curious what would happen:

  #include & lt; Stdio.h & gt; Int Square (Return) * Return Val * } Zero print (zero * PTR) {int (* FTPP) (int, int, int) = (int (*) (int, int, int)) (ptr); Printf ("Call Function. \ N" with 2,4,8 parameters); Printf ("Result:% d \ n", fptr (2,4,8)); } Int main (zero) {print (square); Return 0; }  

It compile and runs without errors or warnings (GCC - Wall on Linux / x86) The output on my system is:

  call function with parameter 2,4,8 result: 4  

Then apparently abandoned unnecessary arguments goes.

Now I want to know what exactly is happening here.

  1. As validity: If I understand the answer correctly, then it is only undefined behavior. So it is true that it runs a good result and produces, just pure luck, right? (Or inconsistency on the part of the compiler authors)
  2. Why would not GCC warn me about this, even with the wall? Is it something that the compiler can detect? Why?

I am coming from Java, where type-checking is very strict, so this behavior puts me in a little confused. Maybe I'm feeling a cultural shock: -)

Additional parameters are omitted, they are appropriately placed on the stack, such as call a function Which is expected for three parameters, however, since your function cares only for one parameter, it only looks at the top of the heap and does not touch other parameters.

The fact is that this call is done, based on the pure facts, two facts:

  • The first parameter for the function and the cast pointer is the same if you To change the function to take a pointer in the string and try to print that string, you will get a good crash because the code will try to address the directory pointer to memory 2.
  • The caller is the default for cleansing the stack, if you change the calling conference, then cleans the Kelly stack, you will end the collar by pushing the three parameters on the stack and then cali Corruption can be stacked by clearing one parameter (or attempting to do it instead).

There is no way that the compiler can warn you about such potential problems for a simple reason - in the general case, it does not know the value of time An indicator on the compilation of, so it can not evaluate what this indicates. Imagine that the class created on the function pointer runtime indicates any method in the virtual table? Therefore, if you tell the compiler that it is an indicator of a function with three parameters, the compiler will trust you.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -