Gcc - c 内存分析
Gcc - c 内存分析
Stack :函数地址,参数,局部非静态变量等
全局区:bss区、data区
Bss未初始化 :
Data初始化 :
堆区:动态库函数申请的,
1: .file "memory.c"
2: .comm dy_a,4,4 (Bss)
3: .globl dy_b
4: .data
5: .align 4
6: .type dy_b, @object(Data)
7: .size dy_b, 4
8: dy_b:
9: .long 90
10: .local st_a
11: .comm st_a,4,4
12: .align 4
13: .type st_b, @object
14: .size st_b, 4
15: st_b:
16: .long 8
17: .comm p,4,4
18: .text
19: .globl function
20: .type function, @function
21: function:
22: pushl %ebp
23: movl %esp, %ebp
24: subl $40, %esp
25: movl $190, -16(%ebp)
26: movl $4, (%esp)
27: call malloc
28: movl %eax, p
29: movl $4, (%esp)
30: call malloc
31: movl %eax, -12(%ebp)
32: leave
33: ret
34: .size function, .-function
35: .data
36: .align 4
37: .type func_st_b.1799, @object
38: .size func_st_b.1799, 4
39: func_st_b.1799:
40: .long 900
41: .local func_st_a.1798
42: .comm func_st_a.1798,4,4
43: .ident "GCC: (GNU) 4.4.6 20120305 (Red Hat 4.4.6-4)"
44: .section .note.GNU-stack,"",@progbits
45: #include<stdlib.h>
46: int dy_a ;
47: int dy_b = 90 ;
48: static int st_a ;
49: static int st_b= 8;
50: int * p ;
51: void function ( int ar)
52: {
53: int func_dy_a ;
54: int func_dy_b = 190 ;
55: int * o ;
56: p = (int *)malloc(sizeof(int)) ;
57: o = (int *)malloc(sizeof(int)) ;
58: static int func_st_a ;
59: static int func_st_b = 900;
60: }
////////////////////////////////////
函数堆栈分配
C 程序:
1: Swap(int a )
2:
3: {
4:
5: a= 90 ;
6:
7: }
8:
9: Int main()
10:
11: {
12:
13: Int b = 888;
14:
15: Swap(b);
16:
17: }
对应的汇编代码
1: file "t3.c"
2:
3: .text
4:
5: .globl swap
6:
7: .type swap, @function
8:
9: swap:
10:
11: pushl %ebp
12:
13: movl %esp, %ebp
14:
15: movl $90, 8(%ebp)
16:
17: popl %ebp
18:
19: ret
20:
21: .size swap, .-swap
22:
23: .globl main
24:
25: .type main, @function
26:
27: main:
28:
29: pushl %ebp
30:
31: movl %esp, %ebp
32:
33: subl $20, %esp
34:
35: movl $888, -4(%ebp)
36:
37: movl -4(%ebp), %eax
38:
39: movl %eax, (%esp)
40:
41: call swap
42:
43: movl $0, %eax
44:
45: leave
46:
47: ret
48:
49: .size main, .-main
50:
51: .ident "GCC: (GNU) 4.4.6 20120305 (Red Hat 4.4.6-4)"
52:
53: .section .note.GNU-stack,"",@progbits
54:
.下面就分析堆栈代码
上面只考虑入栈,至于出栈大家可以自己推出