免费注册 查看新帖 |

Chinaunix

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

memcpy在平台linux-2.6.18上的实现?? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-08 14:30 |只看该作者 |倒序浏览
平台:
Linux localhost 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
Red Hat Enterprise Linux Server release 5.5 (Tikanga)

不懂在这个平台下的memcpy是怎么实现的,以前以为是直接 *dst++ = *src++,从测试来看不是这样,求解释。测试代码如下:


    370 void tst_memmove(int type)
    371 {
    372     char buf1[1024];
    373     memset(buf1, 0, sizeof(buf1));
    374     char* buf2 = buf1 + 1;
    375
    376
    377     strcpy(buf1, "1234567890");
    378
    379     cout << "before operator: "  << endl<< buf1 << endl << buf2 << endl;
    380     if(type == 1)
    381     {
    382         cout << "memmove" << endl;
    383         memmove(buf2, buf1, 6);
    384     }
    385     else if(type == 2)
    386     {
    387         cout << "memcpy" << endl;
    388         memcpy(buf2, buf1, 6);
    389     }
    390     else
    391     {
    392         cout << "my_memcpy" << endl;
    393         my_memcpy(buf2, buf1, 6);
    394     }
    395
    396     cout << "after operator: "  << endl<< buf1 <<  endl;
    397     cout << "============== "  << endl;
    398 }
    399
    400 ///////////////////////////////////////////////////////////////
    401 // main function
    402 ///////////////////////////////////////////////////////////////
    403 typedef xxf::hashmap<int, int> XxfMap;
    404 int main()
    405 {
    406     tst_memmove(1);
    407     tst_memmove(2);
    408     tst_memmove(3);
    409
    410     return 0;
    411 }


===================================out put====================

before operator:
1234567890
234567890
memmove
after operator:
1123456890
==============
before operator:
1234567890
234567890
memcpy
after operator:
1123446890
==============
before operator:
1234567890
234567890
my_memcpy
after operator:
1111111890
==============

论坛徽章:
0
2
发表于 2011-10-08 14:31
对了   补上my_memcpy()的实现:

    362 void my_memcpy(char* dst, const char* src, size_t cnt)
    363 {
    364     while(cnt--)
    365     {
    366         *dst++ = *src++;
    367     }
    368 }

论坛徽章:
49
15-16赛季CBA联赛之福建
日期:2016-06-22 16:22:002015年亚洲杯之中国
日期:2015-01-23 16:25:12丑牛
日期:2015-01-20 09:39:23未羊
日期:2015-01-14 23:55:57巳蛇
日期:2015-01-06 18:21:36双鱼座
日期:2015-01-02 22:04:33午马
日期:2014-11-25 09:58:35辰龙
日期:2014-11-18 10:40:07寅虎
日期:2014-11-13 22:47:15申猴
日期:2014-10-22 15:29:50摩羯座
日期:2014-08-27 10:49:43辰龙
日期:2014-08-21 10:47:58
3 [报告]
发表于 2011-10-08 17:35 |只看该作者
对了   补上my_memcpy()的实现:

    362 void my_memcpy(char* dst, const char* src, size_t cnt)
   ...
xxfihm 发表于 2011-10-08 14:31



    sorry,系统的自动删贴的规则有点过分,给误删除了,现在给您恢复了,带来的不便还请谅解。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
4 [报告]
发表于 2011-10-08 19:16 |只看该作者
回复 1# xxfihm


    這個是 GLibc 實現的,想知道直接看它的源碼。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP