Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do two functions have the same address?

Consider this function template:

template<typename T>
unsigned long f(void *) { return 0;}

Now, I print the addresses of f<A> and f<B> as:

std::cout << (void*)f<A> << std::endl;
std::cout << (void*)f<B> << std::endl;

Why do they print the same address if compiled in MSVS10? Are they not two different functions and therefore should print different addresses?

Updated:

I realized that on ideone, it prints the different address. MSVS10 optimizes the code, as the function doesn't depend on T in any way, so it produces same function. @Mark's answer and comments on this are valuable. :-)

like image 773
Nawaz Avatar asked Feb 17 '12 05:02

Nawaz


People also ask

Can two variables have the same address?

So not to happen all these, two variables can't have same addresses not in c, anywhere, you can transfer a variable address to store in a variable, later you will learn more about this topic in pointers in c language, Hope you understood!!

Can 2 function have same name?

In C you can't have two functions with the same name, at all. In C++, it's entirely possible as long as the function function signature is different, ie two functions having the same name but different set of parameters.

Do functions have address?

We all know that code of every function resides in memory and so every function has an address like all others variables in the program. We can get the address of a function by just writing the function's name without parentheses. Please refer function pointer in C for details.

How do you know if two functions are similar?

Two functions are equal if they have the same domain and codomain and their values are the same for all elements of the domain.


2 Answers

You need to cast to void *:

std::cout << (void*)(ftype*)f<A> << std::endl;
std::cout << (void*)(ftype*)f<B> << std::endl;

If you cast to a function pointer (or several other classes of non-void pointers), it will be interpreted as a bool by the operator<< for std::ostream (hence the 1).

like image 107
bdonlan Avatar answered Nov 06 '22 01:11

bdonlan


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.

like image 21
Mark Ransom Avatar answered Nov 06 '22 00:11

Mark Ransom