免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: notbusy
打印 上一主题 下一主题

为什么char* p指向的内容不能修改? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2003-12-15 15:08 |只看该作者

为什么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的,为什么?

论坛徽章:
0
12 [报告]
发表于 2003-12-15 16:04 |只看该作者

为什么char* p指向的内容不能修改?

char *指向的字符串可以修改:

  1. #include <stdio.h>;  
  2. #include <stdlib.h>;  
  3. #include <string.h>;

  4. int main(void)
  5. {      
  6.         char *p;

  7.         p = (char *)malloc(10);
  8.         strcpy(p,"hello");
  9.         p[0] = 'x';
  10.         printf("%s\n",p);
  11.         free(p);

  12.         return 0;
  13. }
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
13 [报告]
发表于 2003-12-15 17:29 |只看该作者

为什么char* p指向的内容不能修改?

[quote]原帖由 "flightbird"][/quote 发表:

你和他们谈论的不是一个话题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP