为什么在转换函数指针时值会发生变化? (msvc 版本20)
为了测试在文本段中存储值,我编写了下面的代码。 使用调试查看'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;
}
output:
4
00EA1357
00EA1357
00EA1357
00EA1357
00EA1357
c74e9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论