c++ - 将函数地址转换为 UINTPTR_T 给出不正确的结果

标签 c++

我想要变量中 &add1 的值。我尝试了 this answer, 的建议但它没有给出我期望的结果。

uintptr_t add1Address = reinterpret_cast<uintptr_t>(&add1);

此时add1Address等于11669784 ,即0x00B21118

但是我的调试器(并使用 HxD 检查内存)告诉我 &add1 = 0x00B217C0

这里发生了什么?

完整代码如下

#include "stdafx.h"
#include <iostream>

int add1(int num, int num2);

int add1(int num, int num2)
{
        return num + num2;
}

int main(void)
{

    uintptr_t add1Address = reinterpret_cast<uintptr_t>(&add1);
    std::cout << std::hex << add1Address;

}

我在 debugger 中看到的内容在这里

最佳答案

让我大胆猜测一下。

这是因为您可能启用了增量链接。

看看这个:c++ function addresses coming out different in attached profiler library than in the subject code base

关于c++ - 将函数地址转换为 UINTPTR_T 给出不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45107804/

相关文章:

c++ - 运行我的 C++ 代码给我一个空白的控制台

c++ - 增加特定套接字的 TCP 接收窗口

C++11 内存排序——区别?

c++ - 套接字上的 IP_ADD_MEMBERSHIP,套接字也会收听单播吗?

c++ - (初学者)C++关于继承

c++ - 为什么 C++ 没有 ~= 和 != 运算符?

c++ - 使用 CMake 和 AUTORCC 的 Qt 资源文件

c++ - c/c++中perl的hash变量替换

c++ - 在类中重载运算符+

c++ - Qt:小部件在滚动区重叠