免费注册 查看新帖 |

Chinaunix

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

prolj,那个“strcpy”函数这样写,才能更易读 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2009-08-18 15:47 |显示全部楼层
逃避

论坛徽章:
0
2 [报告]
发表于 2009-08-18 20:59 |显示全部楼层

回复 #9 Cyberman.Wu 的帖子

while (*dest++ = *src++); 是数十年来的 proven code,担心是多余的。
写成 inline 80386 assembly 就是:

inline char *strcpy(char *dest, const char *src)
{
    int d0, d1, d2;
    __asm__ __volatile__ (
        "1:\tlodsb\n\t"
        "stosb\n\t"
        "testb %%al,%%al\n\t"
        "jne 1b"
        : "=&S" (d0), "=&D" (d1), "=&a" (d2)
        : "0" (src), "1" (dest)
        : "memory" );
    return dest;
}


这是 MSVCRT 6.0 里面的算法描述:

char *strcpy(char *dst, char *src)
{
    char *cp = dst;

    while (*cp++ = *src++)
        ;               /* Copy src over dst */
    return( dst );
}


可以看到,这样的实现非常清晰直观。参见:http://apuntes.danielcastelao.or ... PLATFORM/STRCAT.ASM

论坛徽章:
0
3 [报告]
发表于 2009-08-18 23:48 |显示全部楼层

回复 #11 Cyberman.Wu 的帖子

I agree. It is rather too stupid to be clever.
Because it costs too much to be clever.
But it's still recommended that you write the code in a smarter style taking full advantage of C language features if it's affordable.



[ 本帖最后由 langue 于 2009-8-18 23:50 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP