c++ - 指针分配的函数地址显示与函数地址不同的值

标签 c++

我有一个函数,说

void myfunc()

如果我声明一个指针并将函数的地址分配给它,如下所示

void *test_pointer = (void*) &myfunc;

然后使用 Visual Studio 进入 Debug模式,我将从监 window 口中获得以下内容

Name                       Value
&myfunc                    0x000000013fc06570
test_pointer               0x000000013fa4786f

现在我希望这两个值相同,为什么不是这样呢?

最佳答案

严格来说,C++ 标准不要求实现支持将函数指针转换为不透明对象指针 (void*)。您可能被告知 void* 可以指向任何东西,但“任何东西”不一定是函数。

由于此功能仅在有条件的情况下受支持,因此无法说明此类转换将如何进行。可能是您的实现只会在 void* 之间进行转换,同时保留值(这是标准位置的最低要求)。但是两个指针表示中的值不必相同。

在此引用最新的 C++ 标准草案,[expr.reinterpret.cast]/8 :

Converting a function pointer to an object pointer type or vice versa is conditionally-supported. The meaning of such a conversion is implementation-defined, except that if an implementation supports conversions in both directions, converting a prvalue of one type to the other type and back, possibly with different cv-qualification, shall yield the original pointer value.

关于c++ - 指针分配的函数地址显示与函数地址不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46612216/

相关文章:

c++ - 我如何在优先队列中找到值(value)?

c++ - 当测试用例数为 1 时,为什么我的程序不接受输入而是打印一个新行?

c++ - 如何在 Visual C++ 中使用#pragma 同时启用两个语言环境?

c# - 进程调试管理器 (PDM) 安全问题

c++ - 在linux中通过C++(或python)访问多个键盘输入

c++ - 等待条件变量后线程未并行运行

c++ - 为 ffmpeg 编码设置 RGB 帧的单个像素

c++ - 阻塞直到 boost::signal 被调用

c++ - 如何在 SFML 中沿对角线移动对象?

java - 如何通过Java输出控制台运行GDB并接受GDB命令?