gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:49:07 24 4
gpt4 key购买 nike

我有一个函数,说

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com