免费注册 查看新帖 |

Chinaunix

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

[C] 字符串比较用==可以吗? [复制链接]

论坛徽章:
1
双子座
日期:2013-11-06 17:18:01
11 [报告]
发表于 2013-12-23 17:47 |只看该作者
十舍七匹狼 发表于 2013-12-23 14:49
可以看下《C语言接口与实现中》的第2章,“原子”,就是介绍的字符串,对于相同长度,内容相同的,会将它们 ...


不能同意你的看法。因为对于常字符串来说,它的地址真的是依编译器的不同而不同的。即使这里比较地址,也不一定相同。

论坛徽章:
0
12 [报告]
发表于 2013-12-23 18:02 |只看该作者
我的回复中说了“会将它们都放在一个地址” 和 “其实就是第一次调用的放在堆上,后续的就跳过”。这两句的目的就是将原本,不管你的字符串时在堆上,还是栈上分配的内存,都通过atom这个程序,在堆上重新分配下,这样可以达到地址唯一的目的!完成了楼主使用“==”比较的目的。这个并不是比较原始程序中的字符串的地址,是一种“再分配”。好处就是,如果多次调用这种字符串,可以显著减少指令次数。可以参见nginx的源码呀,那个里面也有这种封装的!欢迎讨论呀.回复 11# seufy88


   

论坛徽章:
0
13 [报告]
发表于 2013-12-23 18:03 |只看该作者
请参见我回复11楼的。回复 10# lin5161678


   

论坛徽章:
1
摩羯座
日期:2013-12-19 10:04:07
14 [报告]
发表于 2013-12-23 21:52 |只看该作者
回复 13# 十舍七匹狼


   

论坛徽章:
0
15 [报告]
发表于 2013-12-23 21:57 |只看该作者
额,咱俩谈的不是一个话题:
你谈的是2个常量字符串自分配始所在的内存位置
我谈的是字符串通过内存的再分配,完成我上个帖子的目的的同时,顺便把楼主的想法,通过“==”判断字符串相等的目的实现了
您说,是吧..回复 14# lin5161678


   

论坛徽章:
1
天秤座
日期:2014-04-27 07:42:20
16 [报告]
发表于 2013-12-26 14:28 |只看该作者
这个还真不好说,要看实际环境的。不建议这种有潜在风险的写法。

论坛徽章:
0
17 [报告]
发表于 2013-12-26 21:51 |只看该作者
1、未优化:
        .file        "test.c"
        .def        ___main;        .scl        2;        .type        32;        .endef
        .section .rdata,"dr"
LC0:
        .ascii "t\0"
LC1:
        .ascii "yes\0"
LC2:
        .ascii "c\0"
LC3:
        .ascii "ok\0"
LC4:
        .ascii "nook\0"
        .text
        .globl        _main
        .def        _main;        .scl        2;        .type        32;        .endef
_main:
LFB7:
        .cfi_startproc
        .cfi_personality 0,___gxx_personality_v0
        .cfi_lsda 0,LLSDA7
        pushl        %ebp
        .cfi_def_cfa_offset 8
        .cfi_offset 5, -8
        movl        %esp, %ebp
        .cfi_def_cfa_register 5
        andl        $-16, %esp
        subl        $32, %esp
        call        ___main
        movl        $LC0, 28(%esp)
        movl        28(%esp), %eax
        movl        $100, 4(%esp)
        movl        %eax, (%esp)
        call        _strchr
        testl        %eax, %eax
        je        L2
        movl        $LC1, (%esp)
LEHB0:
        call        _printf
        jmp        L3
L2:
        movl        28(%esp), %eax
        cmpl        $LC2, %eax
        jne        L4
        movl        $LC3, (%esp)
        call        _printf
        jmp        L3
L4:
        movl        28(%esp), %eax
        cmpl        $LC0, %eax
        jne        L3
        movl        $LC4, (%esp)
        call        _printf
LEHE0:
L3:
        movl        $0, %eax
        jmp        L8
L7:
        movl        %eax, (%esp)
LEHB1:
        call        __Unwind_Resume
LEHE1:
L8:
        leave
        .cfi_restore 5
        .cfi_def_cfa 4, 4
        ret
        .cfi_endproc
LFE7:
        .def        ___gxx_personality_v0;        .scl        2;        .type        32;        .endef
        .section        .gcc_except_table,"w"
LLSDA7:
        .byte        0xff
        .byte        0xff
        .byte        0x1
        .uleb128 LLSDACSE7-LLSDACSB7
LLSDACSB7:
        .uleb128 LEHB0-LFB7
        .uleb128 LEHE0-LEHB0
        .uleb128 L7-LFB7
        .uleb128 0
        .uleb128 LEHB1-LFB7
        .uleb128 LEHE1-LEHB1
        .uleb128 0
        .uleb128 0
LLSDACSE7:
        .text
        .def        _strchr;        .scl        2;        .type        32;        .endef
        .def        __Unwind_Resume;        .scl        2;        .type        32;        .endef
        .def        _printf;        .scl        2;        .type        32;        .endef

2、 -O2

        .file        "test.c"
        .def        ___main;        .scl        2;        .type        32;        .endef
        .section .rdata,"dr"
LC0:
        .ascii "nook\0"
        .section        .text.startup,"x"
        .p2align 4,,15
        .globl        _main
        .def        _main;        .scl        2;        .type        32;        .endef
_main:
LFB7:
        .cfi_startproc
        pushl        %ebp
        .cfi_def_cfa_offset 8
        .cfi_offset 5, -8
        movl        %esp, %ebp
        .cfi_def_cfa_register 5
        andl        $-16, %esp
        subl        $16, %esp
        call        ___main
        movl        $LC0, (%esp)
        call        _printf
        xorl        %eax, %eax
        leave
        .cfi_restore 5
        .cfi_def_cfa 4, 4
        ret
        .cfi_endproc
LFE7:
        .def        _printf;        .scl        2;        .type        32;        .endef
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP