on Linux, printing function addresses always prints 1 (c++)

空扰寡人 提交于 2019-12-13 18:36:13

问题


I'm developing a c++ program that is dealing with addresses for different variables and functions.

When I compiled my program on a Linux based OS, all functions including the main get the address of 1 instead of an 8 digit hexa number like other variables, which did not happen in Windows.

I wrote this small piece of code to explain the issue

#include <iostream>
using namespace std;
void Function1();
void Function1()
{
}
int main()
{
int tmp;
void (*a) ()=&Function1;
cout<<a<<endl;
cout<<&Function1<<endl;
cout<<&main<<endl;
return 0;
}

for all 3 cout calls, the output is 1 instead of the virtual address.

I really appreciate your help guys

Thanks in Advance Aziz


回答1:


Tthe pointer gets converted to another type, boolean, because it is a function pointer & there is no overloads of operator<< in iostrem library for these types of argument (because there is infinite number of such types). The pointer points to some non-zero address because it has been initialized with address of function - so gets converted to 1 (only 0x0 address would give you boolean 0).

Solution

To assert correct behavior you should cast the pointer to void* so you can use operator<< overload for void* which is

ostream & operator <<( ostream &, const void * );

Example:

void Function1(){}

int main() {
    void ( *a) () = &Function1;
    cout << ( void*)( a) << endl;
    /* or better - being explicit about harshness of this design */
    cout << reinterpret_cast< void*> ( a) <, endl;
}

http://ideone.com/Fne4Mu


C++ Standard n3337 § 4.12 Boolean conversions [conv.bool]

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.




回答2:


<< has no standard overload taking a function pointer; so instead, the pointer is converted to bool (since that's a legitimate implicit conversion), giving 1, or true if you've used the std::boolalpha manipulator on the stream.

If you want the address, you'll have to explicitly convert it to an object pointer:

std::cout << reinterpret_cast<void*>(&Function1) << std::endl;



回答3:


If I change your code to the following, the function pointer addresses will be displayed correctly:

void Function1() {
}

int main() {
    void*a = (void*)&Function1;
    cout<<a<<endl;
    cout<< (void*)&Function1<<endl;
    cout<< (void*)&main<<endl;
    return 0;
}

Output:

0x8048710
0x8048710
0x8048570

See the working sample here please.


The problem is, there's a standard operator overload available for

ostream& operator<<(ostream&,void*)

but not for function pointers

ostream& operator<<(ostream&,void (Function1Type*)())

and the least valid conversion draws

ostream& operator<<(ostream&,bool)

where everything other than 0x00000000 is true.



来源:https://stackoverflow.com/questions/25538776/on-linux-printing-function-addresses-always-prints-1-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!