Reputation: 18824
Isn't the size of a function's address known at compilation type ?
Upvotes: 1
Views: 2293
Reputation: 137910
Arithmetic operations on a pointer treats the pointer as an array of objects of a given type. So when you add 3 to an int *
, you advance by three int
s.
Function pointers cannot be interpreted as arrays of anything. Instructions, maybe, or maybe not. Some machines have separate a address space for instructions. Some machines have variable-length instructions.
As an aside, the size of the function is known at compile time, but only after the compiler has finished its work. Compiling the size of a function into itself can be tricky in assembly language, the only sort of programming with any hope of forming such a construct.
Upvotes: 7
Reputation: 3462
C does not allow or define arithmetic on function pointers. You should have a look here. It is because it will not be defined where you will reach in the code when you do addition and subtraction operation on the function pointers. Just think that you are compiling the same C code for different hardware architectures. When you increment or decrement function pointers they might point to completely different part of code (or instructions as you say) which will be inconsistent across the platforms.
Upvotes: -2
Reputation: 409412
You can only perform arithmetic on related pointers. For example if you have a buffer with multiple pointers into different positions of that buffer, you can perform arithmetic on those pointers. However, if you try to perform pointer arithmetic on two unrelated pointers, like two pointers that point to different buffers, then that is undefined behavior.
A function pointer can not be related to anything except another pointer to the same function.
Upvotes: 1