Multiple Function Pointers

Are there any guarantees that functions that differ only in their names (and not the parameters and return type) cannot share the same address in C and C ++? I do not see anything in this standard.

#include <cassert> void foo() {} void bar() {} int main() { assert(foo != bar); } 
+7
source share
4 answers

The C ++ 11 standard says:

5.10 Equality Operators
Pointers of the same type (after conversion of pointers) can be compared for equality. Two pointers of the same type compare the same if and only if both are equal to zero, both indicate the same function or both represent the same address (3.9.2).

If you do not have function pointers, they may have the same address, but we would not know. If you are comparing pointers to two different functions, they should not compare the same.


One of the reasons for the confusion may be that MSVC compilers, as you know, combine code for template functions that produce identical machine code for different types (for example, int and long ). It does not meet the requirements.

However, this applies to functions with different signatures, and not to which particular issue.

+7
source

Yes. C99 6.5.10: 6

Two pointers compare the same if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at the beginning) or a function , ...

Edit: the rest of the paragraph, as it matters:

both are pointers to one last element of the same array, or one is a pointer to one end of the end of one array object and the other is a pointer to the beginning of another array object that occurs immediately after the first array object in the address space.

What I take from him:

  • equality applied to irregular operands is unspecified in many words. This may be random for extraneous reasons, but it is not undefined if you use it < undefined incorrectly when you use it incorrectly.
  • as I understand it, the rest of the paragraph does not apply to functions, because the function is not an object and cannot be an element of an array.
+3
source

Many compilers with optimizations enabled will have two identical addresses. For example, from msdn :

/ OPT: ICF may cause the same address to be assigned to other functions or read-only data (constant variables compiled with / Gr). So, / OPT: ICF can break the program, which depends on the address of the function or read-only data members differ. See / Gy (Enable Link to Functional Level) for more information.

ICF Identical code folding

+2
source

Information about the implementation.

(C99, 5.1.2.3p1) "The semantic descriptions in this International Standard describe the behavior of an abstract machine in which optimization problems do not matter."

+1
source

All Articles