为什么在转换函数指针时值会发生变化? (msvc 版本20)

发布于 2025-01-11 16:18:31 字数 1031 浏览 1 评论 0原文

为了测试在文本段中存储值,我编写了下面的代码。 使用调试查看'foo'的值和'v'、'bar'的值是不同的。没有“__declspec(naked)”的普通函数的值也会改变。 当使用“foo”的地址调试内存时,“12345678”显示良好。 如果您能告诉我为什么这些值会改变,我将不胜感激。

编辑:
当我再次检查时,调试中显示的“foo”值唯一与程序的值或输出不同。但我还是不知道为什么会不一样。

代码:

__declspec(naked) void foo() {
    __asm {
        _emit 0x78
        _emit 0x56
        _emit 0x34
        _emit 0x12
    }
}

int main() {
    cout << sizeof(&foo) << '\n';

    void (*bar)() = foo;
    cout << bar << '\n';

    void* v = (void*)(&foo);
    cout <<  v << '\n';

    unsigned* p = (unsigned*)(v);
    cout << p << '\n';

    unsigned a = *p;
    cout << hex <<  a;

    cout << (void(*)())foo;
    cout << foo;
}

在此处输入图像描述

输出:

4
00EA1357
00EA1357
00EA1357
00EA1357
00EA1357
c74e9

To test storing a value in a text segment, I wrote the code below.
The value of 'foo' and the value of 'v', 'bar' are different when viewed using debugging. Normal functions without '__declspec(naked)' also have their values changed.
When debugging memory using the address of 'foo', '12345678' is displayed well.
I'd appreciate it if you could tell me why the values change.

edit:
When I checked again, the only thing that the 'foo' value displayed in debugging was different from the value or output of the program. But I still don't know why it's different.

code:

__declspec(naked) void foo() {
    __asm {
        _emit 0x78
        _emit 0x56
        _emit 0x34
        _emit 0x12
    }
}

int main() {
    cout << sizeof(&foo) << '\n';

    void (*bar)() = foo;
    cout << bar << '\n';

    void* v = (void*)(&foo);
    cout <<  v << '\n';

    unsigned* p = (unsigned*)(v);
    cout << p << '\n';

    unsigned a = *p;
    cout << hex <<  a;

    cout << (void(*)())foo;
    cout << foo;
}

enter image description here

output:

4
00EA1357
00EA1357
00EA1357
00EA1357
00EA1357
c74e9

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文