Domanda

Se compilo:

int *a;

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

e quindi smontare il principale in cdb ottengo:

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    
.

Allora * A è simboleggiato dai puntatoriProject! A.Tutto buono.

Tuttavia, se dichiaro il puntatore entro il principale:

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

Vedo che A è solo un compensato dal puntatore dello stack (credo), piuttosto la struttura leggibile dall'uomo che mi aspetterei (come, dire i puntatori di proiezione! 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
.

Questo è probabilmente tanto sulla mia comprensione di ciò che il compilatore è fatto come qualsiasi altra cosa, ma qualcuno può spiegare perché la notazione per A non è ciò che mi aspetto?

(questo ispirato al musing mentre guarda il debug di Windows X64: Fondazioni pratiche di Dmitry Vostokov).

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top