简体   繁体   English

基本的cdb:cdb会改变它的范围吗?

[英]Basic cdb: does cdb vary how it notates scope?

If I compile: 如果我编译:

int *a;

void main(void)
{
    *a = 1;
}

and then disassemble main in cdb I get: 然后在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    

So *a is symbolized by pointersproject!a. 所以* a由pointersproject表示!a。 All good. 都好。

However, if I declare the pointer within main: 但是,如果我在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): 我看到a只是偏离堆栈指针(我相信),而不是我期望的人类可读结构(比如说,指针项目!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? 这可能与我对编译器所做的事情的理解同样重要,但是:有人可以解释为什么a的符号不是我所期望的吗?

(This inspired by musing while looking at x64 Windows Debugging: Practical Foundations by Dmitry Vostokov). (这是受到沉思的启发,同时看着D64ry Vostokov的x64 Windows调试:实用基础)。

When a variable is defined inside a function, it is an automatic variable unless explicitly declared static. 当在函数内定义变量时,除非显式声明为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. 如果创建静态,它将不会在堆栈中分配,即使其范围是函数main。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM