Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers of several functions

Tags:

c++

c

Is there any guarantees that the functions which differs only by its names (not parameters and return type also) can't share the same address in C and C++? I don't see anything about it in the standard.

#include <cassert>

void foo() {}

void bar() {}

int main()
{
    assert(foo != bar);
}
like image 904
FrozenHeart Avatar asked Aug 28 '12 20:08

FrozenHeart


People also ask

Can you pass pointers to functions?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

What is the use of function pointers in C?

Function pointers in C can be used to create function calls to which they point. This allows programmers to pass them to functions as arguments. Such functions passed as an argument to other functions are also called callback functions.

Can a variable have multiple pointers?

then you can declare multiple int-pointer variables by simply giving the int-pointer type (int *) followed by a comma-separated list of names to use for the variables ( ptr_a, ptr_b ).

How do you define a function pointer?

Function Pointer Syntaxvoid (*foo)( int ); In this example, foo is a pointer to a function taking one argument, an integer, and that returns void. It's as if you're declaring a function called "*foo", which takes an int and returns void; now, if *foo is a function, then foo must be a pointer to a function.


2 Answers

The C++11 standard says

5.10 Equality operators
Pointers of the same type (after pointer conversions) can be compared for equality. Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address (3.9.2).

If you don't have any pointers to the functions, they just might have the same address, but we wouldn't know. If you are comparing pointers to two different functions, they must not compare equal.


One cause for confusion might be that the MSVC compilers are known to combine code for template functions that happen to produce identical machine code for different types (like int and long). This is not compliant.

However, this is for functions with different signatures, and not exactly what this question is about.

like image 200
Bo Persson Avatar answered Sep 20 '22 23:09

Bo Persson


Yes. C99 6.5.10:6

Two pointers compare equal 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 its beginning) or function, …

Edit: the rest of the paragraph, since it turns out to have some importance:

both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.

What I take from it:

  • equality applied to the wrong operands is, in so many words, unspecified. It can be accidentally true for extraneous reasons but it is not undefined when you use it wrong the way < is undefined when you use it wrong.
  • to the best of my understanding, the rest of the paragraph does not apply to functions, because a function neither is an object nor can it be an element of an array.
like image 20
Pascal Cuoq Avatar answered Sep 21 '22 23:09

Pascal Cuoq