免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 9865 | 回复: 40
打印 上一主题 下一主题

[C] 大师请进,在linux上,好像无法对栈上的内容进行修改? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-27 14:23 |只看该作者 |倒序浏览
我刚写一个lowercase的函数,但发现当输入是“char* dest = " HELlo WosLD ";”时执行到“string += 32;”,linux会抛那个段错误,而当输入已经malloc了,是不会出现这个段错误的,请问各位大师,有什么办法避免这个段错误?
  1. char* lowercase(char string[]){
  2.         int i=0;
  3.         char* res = string;
  4.         for(i=0;string[i];i++){
  5.                 string[i] += 32;
  6.         }
  7.         return (res);
  8. }

  9. int main(void){
  10.         char* dest = " HELlo WosLD ";
  11.         dest = lowercase(dest);
  12.         printf("the string after lowercase:%s\n",dest);

  13. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-06-27 14:25 |只看该作者
那是rodata

论坛徽章:
0
3 [报告]
发表于 2010-06-27 14:39 |只看该作者
那是rodata
prolj 发表于 2010-06-27 14:25



    嗯,而且不是栈上数据。

论坛徽章:
0
4 [报告]
发表于 2010-06-27 14:48 |只看该作者
本帖最后由 ikewu83 于 2010-06-27 14:51 编辑

先谢谢了,那么一般在写类似Lowercase函数的时候,假设它的输入不是rodata吗?有什么更变通的方法?

论坛徽章:
0
5 [报告]
发表于 2010-06-27 14:51 |只看该作者
char dest[] = " HELlo WosLD ";

论坛徽章:
0
6 [报告]
发表于 2010-06-27 14:57 |只看该作者
回复 5# prolj


    这个好像会跑错。我的问题是我们一定要假设类似lowercase函数的输入不是rodata吗?有什么更变通的方法?

论坛徽章:
0
7 [报告]
发表于 2010-06-27 17:54 |只看该作者
汇编

  1. .file        "hack1.c"
  2.         .text
  3. .globl lowercase
  4.         .type        lowercase, @function
  5. lowercase:
  6.         pushl        %ebp
  7.         movl        %esp, %ebp
  8.         subl        $16, %esp
  9.         movl        $0, -4(%ebp)
  10.         movl        8(%ebp), %eax
  11.         movl        %eax, -8(%ebp)
  12.         movl        $0, -4(%ebp)
  13.         jmp        .L2
  14. .L3:
  15.         movl        -4(%ebp), %eax
  16.         addl        8(%ebp), %eax
  17.         movl        -4(%ebp), %edx
  18.         addl        8(%ebp), %edx
  19.         movzbl        (%edx), %edx
  20.         addl        $32, %edx
  21.         movb        %dl, (%eax)
  22.         addl        $1, -4(%ebp)
  23. .L2:
  24.         movl        -4(%ebp), %eax
  25.         addl        8(%ebp), %eax
  26.         movzbl        (%eax), %eax
  27.         testb        %al, %al
  28.         jne        .L3
  29.         movl        -8(%ebp), %eax
  30.         leave
  31.         ret
  32.         .size        lowercase, .-lowercase
  33.         .section        .rodata
  34. .LC0:
  35.         .string        " HELlo WosLD "
  36.         .align 4
  37. .LC1:
  38.         .string        "the string after lowercase:%s\n"
  39.         .text
  40. .globl main
  41.         .type        main, @function
  42. main:
  43.         pushl        %ebp
  44.         movl        %esp, %ebp
  45.         andl        $-16, %esp
  46.         subl        $32, %esp
  47.         movl        $.LC0, 28(%esp)
  48.         movl        28(%esp), %eax
  49.         movl        %eax, (%esp)
  50.         call        lowercase
  51.         movl        %eax, 28(%esp)
  52.         movl        $.LC1, %eax
  53.         movl        28(%esp), %edx
  54.         movl        %edx, 4(%esp)
  55.         movl        %eax, (%esp)
  56.         call        printf
  57.         leave
  58.         ret
  59.         .size        main, .-main
  60.         .ident        "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
  61.         .section        .note.GNU-stack,"",@progbits
复制代码
你自己看吧
.L0就是你的字符串的地址
在只读段
我懒得看了,你自己看吧

论坛徽章:
0
8 [报告]
发表于 2010-06-27 18:14 |只看该作者
C已经不能满足我了,我要玩儿C++。

论坛徽章:
0
9 [报告]
发表于 2010-06-27 19:21 |只看该作者
回复 8# prolj

阴雨阿,阴雨阿!

论坛徽章:
0
10 [报告]
发表于 2010-06-27 20:12 |只看该作者
Lz的c语言的写法就是不规范的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP