// 剪輯自: http://www.runoob.com/w3cnote/cpp-static-usage.html
#include <stdio.h>
#include <stdlib.h>
//全局外部有初值
int k1 = 1;
//外部無初值
int k2;
static int k3 = 2;
static int k4;
int main()
{
static int m1 = 2, m2;
//棧區-變數位址
int i = 1;
char*p;
char str[10] = "hello";
char*q = "hello";
p = (char *)malloc(100); //堆區位址-動態申請
free(p);
printf("棧區-變數位址 i :%p\n", &i);
printf("棧區-變數位址 p :%p\n", &p);
printf("棧區-變數位址str :%p\n", str);
printf("棧區-變數位址 q :%p\n", &q);
printf("堆區位址-動態申請:%p\n", p);
printf("全局外部有初值 k1:%p\n", &k1);
printf("外部無初值 k2 k2 :%p\n", &k2);
printf("外部有初值 k3 :%p\n", &k3);
printf("外靜無初值 k44 :%p\n", &k4);
printf("內靜態有初值 m1 :%p\n", &m1);
printf("內靜態無初值 m2 :%p\n", &m2);
printf("文字常量地址 :%p, %s\n", q, q);
printf("程式區位址 :%p\n", &main);
return 0;
}
結果:
棧區-變數位址 i :0018FF44
棧區-變數位址 p :0018FF40
棧區-變數位址str :0018FF34
棧區-變數位址 q :0018FF30
堆區位址-動態申請:00231C80
全局外部有初值 k1:00422A30
外部無初值 k2 k2 :004235B8
外部有初值 k3 :00422A34
外靜無初值 k44 :004235B4
內靜態有初值 m1 :00422A38
內靜態無初值 m2 :004235B0
文字常量地址 :0042016C, hello
程式區位址 :00401005
Disassembly:
@ILT+0(_main):
00401005 jmp main (00401010)
0040100A int 3
0040100B int 3
0040100C int 3
0040100D int 3
0040100E int 3
0040100F int 3
9: {
00401010 push ebp
00401011 mov ebp,esp
00401013 sub esp,58h
00401016 push ebx
00401017 push esi
00401018 push edi
00401019 lea edi,[ebp-58h]
0040101C mov ecx,16h
00401021 mov eax,0CCCCCCCCh
00401026 rep stos dword ptr [edi]
10: static int m1 = 2, m2;
11: int i = 1;
00401028 mov dword ptr [ebp-4],1
12: char*p;
13: char str[10] = "hello"; memory 查看
0040102F mov eax,[string "hello" (0042016c)]
00401034 mov dword ptr [ebp-14h],eax
00401037 mov cx,word ptr [string "hello"+4 (00420170)]
0040103E mov word ptr [ebp-10h],cx
00401042 xor edx,edx
00401044 mov dword ptr [ebp-0Eh],edx
14: char*q = "hello";
00401047 mov dword ptr [ebp-18h],offset string "hello" (0042016c)
15: p = (char *)malloc(100);
0040104E push 64h 10->16
00401050 call malloc (00401240)
00401055 add esp,4
00401058 mov dword ptr [ebp-8],eax
16: free(p);
0040105B mov eax,dword ptr [ebp-8]
0040105E push eax
0040105F call free (00401c80)
00401064 add esp,4
17: printf("棧區-變數位址 i :%p\n", &i);
00401067 lea ecx,[ebp-4]
0040106A push ecx
0040106B push offset string "-} iG%p\n" (00420154)
00401070 call printf (004011c0)
00401075 add esp,8
18: printf("棧區-變數位址 p :%p\n", &p);
00401078 lea edx,[ebp-8]
0040107B push edx
0040107C push offset string "~?qa} pG%p\n" (0042013c)
00401081 call printf (004011c0)
00401086 add esp,8
19: printf("棧區-變數位址str :%p\n", str);
00401089 lea eax,[ebp-14h]
0040108C push eax
0040108D push offset string "-}strG%p\n" (00420124)
00401092 call printf (004011c0)
00401097 add esp,8
20: printf("棧區-變數位址 q :%p\n", &q);
0040109A lea ecx,[ebp-18h]
0040109D push ecx
0040109E push offset string "-} q G%p\n" (0042010c)
004010A3 call printf (004011c0)
004010A8 add esp,8
21: printf("堆區位址-動態申請:%p\n", p);
004010AB mov edx,dword ptr [ebp-8]
004010AE push edx
004010AF push offset string "}-AG%p\n" (00
004010B4 call printf (004011c0)
004010B9 add esp,8
22: printf("全局外部有初值 k1:%p\n", &k1);
004010BC push offset _k1 (00422a30)
004010C1 push offset string "~ k1G%p\n" (00420
004010C6 call printf (004011c0)
004010CB add esp,8
23: printf("外部無初值 k2 k2 :%p\n", &k2);
004010CE push offset _k2 (004235b8)
004010D3 push offset string " ~? k2G%p\n" (004200bc)
004010D8 call printf (004011c0)
004010DD add esp,8
24: printf("外部有初值 k3 :%p\n", &k3);
004010E0 push offset k3 (00422a34)
004010E5 push offset string "??~ k3G%p\n" (004200a0)
004010EA call printf (004011c0)
004010EF add esp,8
25: printf("外靜無初值 k44 :%p\n", &k4);
004010F2 push offset k4 (004235b4)
004010F7 push offset string " ~?? k4G%p\n" (00420088)
004010FC call printf (004011c0)
00401101 add esp,8
26: printf("內靜態有初值 m1 :%p\n", &m1);
00401104 push offset m1 (00422a38)
00401109 push offset string " ??? m1G%p\n" (00420070)
0040110E call printf (004011c0)
00401113 add esp,8
27: printf("內靜態無初值 m2 :%p\n", &m2);
00401116 push offset m2 (004235b0)
0040111B push offset string " ???? m2G%p\n" (00420058)
00401120 call printf (004011c0)
00401125 add esp,8
28: printf("文字常量地址 :%p, %s\n", q, q);
00401128 mov eax,dword ptr [ebp-18h]
0040112B push eax
0040112C mov ecx,dword ptr [ebp-18h]
0040112F push ecx
00401130 push offset string " r`qa}G%p, %s\n" (00420038)
00401135 call printf (004011c0)
0040113A add esp,0Ch
29: printf("程式區位址 :%p\n", &main);
0040113D push offset @ILT+0(_main) (00401005)
00401142 push offset string " {?a}G%p\n" (0042001c)
00401147 call printf (004011c0)
0040114C add esp,8
30: return 0;
0040114F xor eax,eax
31: }
00401151 pop edi
00401152 pop esi
00401153 pop ebx
00401154 add esp,58h
00401157 cmp ebp,esp
00401159 call __chkesp (004030d0)
0040115E mov esp,ebp
00401160 pop ebp
00401161 ret