Do Distinct Functions Have Distinct Addresses

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.

Is an implementation allowed to site two identical function definitions at the same address, or not?

Well, look at the statement logically. You have three clauses:

  1. Both are null.

  2. Both point to the same function.

  3. Both have the same address.

These clauses are joined by a logical "or". Therefore, if any one of these is true, then the two pointers are allowed to compare equal. If a compiler so decides, it is possible to fail #3 yet still pass #2. Logical "or" means that such pointers would compare equal.

Also, it should be noted that member pointers do not have an "address" in the traditional sense. They do have a value, but it's not a memory address. That's why you're not allowed to cast them to void* and so forth.

The passage guarantees, given function pointers t and u, if t == u, that t(...); shall cause the same behavior as u(...);. That behavior will either be referencing NULL, calling the same function, or executing the code at the same address. Thus, the same behavior is had.

Technically, Mehrdad's problem is that he's getting the same value from two different member function names. So #3 applies. I don't see anything in the standard that requires that different member function names return distinct values when getting functions for them.

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.

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 C functions guaranteed to have a fixed memory address?

Per 6.5.9:

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, 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.

(Boldface added for emphasis.)

SQL Window Function to get addresses with more than 1 unique last name present (Snowflake)

I would like the query to return only 1 row in that example: 10 lake road.

This sounds like aggregation:

SELECT a.address, count(*)
FROM clients_addresses a
WHERE a.state = 'FL'
GROUP BY a.address
HAVING COUNT(DISTINCT a.lname) > 1;

If you want the original rows (which is not what your question asks for), you can use:

SELECT a.*
FROM clients_addresses a
WHERE a.state = 'FL'
QUALITY COUNT(DISTINCT a.lname) OVER (PARTITION BY a.address) > 1;


Related Topics



Leave a reply



Submit