我正在尝试使用下面代码中所示的 printf
语句打印函数指针地址两次...
class B
{
public:
int fun()
{
}
};
int main()
{
int (B::*pb)()=&B::fun;
printf("ptr:%x | %x\n",pb,pb); //Output is ptr:8048730 | 0
}
当我将相同的变量传递给 printf
时,它应该打印相同的值,但在得到结果后我感到很惊讶。
任何人都可以解释这个或我做错的地方的原因。
gcc 版本 4.8.2 (GCC)
最佳答案
如果你打开警告(将 -Wall
标志传递给编译器),编译器会告诉你你做错了什么:
warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int (B::*)()’ [-Wformat]
warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 3 has type ‘int (B::*)()’ [-Wformat]
一般来说,我只能鼓励你打开警告。
现在一个 hack(未定义的行为)如下:
std::printf("ptr:%p | %p\n",(void*)funcptr,(void*)funcptr);
这仍然会给出以下警告:
警告:从‘int (B::*)()’转换为‘void*’[-Wpmf-conversions]
但是程序如您所愿打印了两次相同的地址。它仍然是未定义的行为,请参阅 How to format a function pointer?按照公认的答案(无耻地从那里窃取代码),可以按如下方式打印地址:
#include <cstdio>
class B {
public:
int fun() { return 0; }
};
int main()
{
int (B::*funcptr)()=&B::fun;
unsigned char *p = (unsigned char *)&funcptr;
for (unsigned int i = 0; i < sizeof funcptr; ++i)
{
std::printf("%02x ", p[i]);
}
std::putchar('\n');
return 0;
}
根据该答案,这是实现此目的的唯一合法方法。它不会发出任何警告。
关于c++ - 使用指针打印函数地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808381/