基本 cdb:cdb 表示范围的方式是否有所不同?
如果我编译:
int *a;
void main(void)
{
*a = 1;
}
然后在 cdb 中反汇编 main,我得到:
pointersproject!main:
00000001`3fd51010 mov rax,qword ptr [pointersproject!a (00000001`3fd632f0)]
00000001`3fd51017 mov dword ptr [rax],1
00000001`3fd5101d xor eax,eax
00000001`3fd5101f ret
所以 *a 由pointersproject!a 表示。一切都好。
但是,如果我在 main: 中声明指针:
void main(void)
{
int *a;
a = 1;
}
我看到 a 只是堆栈指针的偏移量(我相信),而不是我期望的人类可读结构(例如,pointersproject!main!a)
pointersproject!main:
00000001`3fd51010 sub rsp,18h
00000001`3fd51014 mov rax,qword ptr [rsp]
00000001`3fd51018 mov dword ptr [rax],1
00000001`3fd5101e xor eax,eax
00000001`3fd51020 add rsp,18h
00000001`3fd51024 ret
:可能与我对编译器所做的事情的理解和其他任何事情一样重要,但是:任何人都可以解释为什么 a 的表示法不是我所期望的吗?
(这受到德米特里·沃斯托科夫 (Dmitry Vostokov) 的《x64 Windows 调试:实用基础》的思考启发。
If I compile:
int *a;
void main(void)
{
*a = 1;
}
and then disassemble main in cdb I get:
pointersproject!main:
00000001`3fd51010 mov rax,qword ptr [pointersproject!a (00000001`3fd632f0)]
00000001`3fd51017 mov dword ptr [rax],1
00000001`3fd5101d xor eax,eax
00000001`3fd5101f ret
So *a is symbolized by pointersproject!a. All good.
However, if I declare the pointer within main:
void main(void)
{
int *a;
a = 1;
}
I see that a is just an offset from the stack pointer (I believe), rather then the human-readable structure I'd expect (like, say pointersproject!main!a):
pointersproject!main:
00000001`3fd51010 sub rsp,18h
00000001`3fd51014 mov rax,qword ptr [rsp]
00000001`3fd51018 mov dword ptr [rax],1
00000001`3fd5101e xor eax,eax
00000001`3fd51020 add rsp,18h
00000001`3fd51024 ret
This is probably as much about my understanding of what the compiler's done as anything else but: can anyone explain why the notation for a isn't what I expect?
(This inspired by musing while looking at x64 Windows Debugging: Practical Foundations by Dmitry Vostokov).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当变量在函数内部定义时,除非显式声明为静态,否则它是自动变量。此类变量仅在函数执行期间存在,并且通常在堆栈中分配,因此当函数退出时它们将被释放。您在编译的代码中看到的变化不是由于范围的变化而是由于从静态变量到自动变量的变化。如果你创建了一个静态变量,即使它的作用域是 main 函数,它也不会在堆栈中分配。
When a variable is defined inside a function, it is an automatic variable unless explicitly declared static. Such variables only live during the execution of the function and are normally allocated in the stack, thus they are deallocated when the function exits. The change you see in the complied code is not due to the change in scope but to the change from static to automatic variable. If you make a static, it will not be allocated in the stack, even if its scope is the function main.