Why Do Two Functions Have the Same Address

Why do two functions have the same address?

Since the function doesn't depend on the template parameter, the compiler can condense all instantiations into a single function.

I don't know why you get 1 for the address.



Added by Nawaz:

I experimented with my real code, and concluded that what @Mark said above is very important here :

Since the function doesn't depend on the template parameter, the compiler can condense all instantiations into a single function.

I also came to a conclusion that if the function-body depends on T*, not on T, it still produces the same function for different type arguments in my real code (not on ideone, though). However, if it depends on T, then it produces different functions, because sizeof(T) differs (fortunately for me) for different type arguments.

So I added a dummy automatic variable of type T in the function template, so that the function could depend on the size of T so as to force it to produce different functions.

Make two functions have the same memory address in C++

It's platform specific. For GCC, you can do this:

extern "C" {
void f1(params);
void f2(params) __attribute__((weak, alias("f1")));
}

Ref: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

Do distinct functions have distinct addresses?

5.10 Equality operators [expr.eq]


1 The == (equal to) and the != (not equal to) operators group left-to-right. The operands shall have arithmetic, enumeration, pointer, or pointer to member type, or type std::nullptr_t. The operators == and != both yield true or false, i.e., a result of type bool. In each case below, the operands shall have the same type after the specified conversions have been applied.

2 If at least one of the operands is a pointer, pointer conversions (4.10) and qualification conversions (4.4) are performed on both operands to bring them to their composite pointer type (Clause 5). Comparing pointers is defined as follows: Two pointers compare equal if they are both null, both point to the same function, or both represent the same address (3.9.2), otherwise they compare unequal.

Let's take the last bit-for-bit:

  1. Two null pointers compare equal.

    Good for your sanity.
  2. Two pointers to the same function compare equal.

    Anything else would be extremely surprising.

    It also means that only one out-of-line version of any inline-function may ever have its address taken, unless you want to make function-pointer comparisons prohibitively complicated and expensive.
  3. Both represent the same address.

    Now that one is what it's all about. Dropping this and reducing if and only if to a simple if would leave it to interpretation, but that's a clear mandate to make any two functions identical, as long as it does not otherwise change observable behavior of a conformant program.

Inline functions and variables have same address?

What does it mean that functions have the same address?

Exactly what it says. Every same inline function in every translation unit has the same address, i.e. it is only one same function. Why this is important you'll see.

And does this also mean that inline variables all have the same addresses, just as if it was one variable?

Exactly.

If so, why even use inline on variables?

It's like asking why use inline functions. You use inline functions for functions defined in header files, because they are going to be included multiple times in multiple translation units. If they were not inline, the linker will see multiple definitions of the same exact function, and it will complain about duplicate symbols. It doesn't know that the functions are actually the exact same function.

inline comes in here. By marking a function inline, you tell the compiler that it is always the same function, even across multiple translation units. The same thing goes for variables which are defined in header files. They too are included in multiple translation units, and the linker doesn't know that they should refer to the same exact variable. inline fixes this once again.

pointers with two similar variables in two different functions

If those functions do not call each other, it's possible that calling one, the address will be x, then if the function returns (and the variable does not escape) and the other function is called, the same x address may be reused; but only if the other variable is not live (reachable) anymore. Whether this may be the case is not clear from your question.

Go has automatic memory management. Unless you touch package unsafe, you should not worry about pointers and addresses, it's safe to take and even to return addresses of local variables. The same memory will not be reused if a variable using that memory is still reachable.

One exception is variables that have 0 size. As per the spec, variables having zero size may or may not have the same address. In practice this causes no trouble as you can't change the value of such variables as they cannot store different values, but you can't assume anything about their memory addresses.

Are two function pointers to the same function always equal?

Section §5.10/1 of the C++11 standard says:

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

Two copies of the same inline function are still the same function. From an implementation point-of-view, the compiler will generate a copy of the function in each translation unit but the linker will then throw one of the copies away so only one is remaining.

By taking the address of a function you prevent it from being inlined (different from inline, which is more about avoiding violation of the One Definition Rule).

DLLs are outside the scope of the standard but only one copy of the function will remain in the binary image so getting the function address (e.g. GetProcAddress) from the DLL will get the same function pointer as code inside the DLL.

What is the technical term for two functions with the same signature and same behavior, but different names?

Do you mean two functions like this?

f: T -> R
g: T -> R

For All x in T, f(x) == g(x)

And accepting also same side effects (in math there are no side effects of a function)

g and f are just equivalent functions.



Related Topics



Leave a reply



Submit