- 论坛徽章:
- 0
|
为什么char* p指向的内容不能修改?
1 #include <stdio.h>;
2 #include <stdlib.h>;
3 char a[] = "hello";
4 char* p = "hello";
5
6
7 int main(void)
8 {
9 return 0;
10 }
编译结果为:
1 .file "test.c"
2 .version "01.01"
3 gcc2_compiled.:
4 .globl a <----------------
5 .data <-----------------
6 .type a,@object
7 .size a,6
8 a:
9 .string "hello"
10 .globl p <----------------
11 .section .rodata <----------------
12 .LC0:
13 .string "hello"
14 .data
15 .align 4
16 .type p,@object
17 .size p,4
18 p:
19 .long .LC0 <----------------
20 .text
21 .align 4
22 .globl main
23 .type main,@function
24 main:
25 pushl %ebp
26 movl %esp, %ebp
27 movl $0, %eax
28 popl %ebp
29 ret
30 .Lfe1:
31 .size main,.Lfe1-main
32 .ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.32.96-110)"
可见a和p都是全局的。p指向的数据存在section里并是rdata;a的数据在data里且未被保护。这里的section是什么意思?表示.LC0是存在哪里的?
1 #include <stdio.h>;
2 #include <stdlib.h>;
3
4
5 int main(void)
6 {
7 char a[] = "hello";
8 char* p = "hello";
9 return 0;
10 }
编译的结果为:
1 .file "test.c"
2 .version "01.01"
3 gcc2_compiled.:
4 .section .rodata
5 .LC0: <-------------------
6 .string "hello"
7 .text
8 .align 4
9 .globl main
10 .type main,@function
11 main:
12 pushl %ebp
13 movl %esp, %ebp
14 subl $40, %esp
15 leal -8(%ebp), %eax
16 subl $16, %eax
17 movl .LC0, %eax <------------------
18 movl %eax, -24(%ebp)
19 movw .LC0+4, %ax
20 movw %ax, -20(%ebp)
21 movl $.LC0, -28(%ebp) <--------------
22 movl $0, %eax
23 leave
24 ret
25 .Lfe1:
26 .size main,.Lfe1-main
27 .ident "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.3 2.96-110)"
~
可见a的内容和p指向的内容都是LC0,LC0放在section里且被保护。但对a[0]修改没有coredump,但对*p修改是coredump的,为什么? |
|