当前位置: 首页 > news >正文

网上做名片的网站小游戏推广接单平台

网上做名片的网站,小游戏推广接单平台,做网站大优惠,网站备案填写目录 引言一、库函数介绍二、库函数详解三、源码实现1.memcpy源码实现2.memmove源码实现 四、测试1.memcpy函数2.memmove函数 五、源码1.memcpy源码2.memmove源码 六、参考文献 引言 关于memcpy和memmove这两个函数,不论是算法竞赛还是找工作面试笔试,对…

目录

  • 引言
  • 一、库函数介绍
  • 二、库函数详解
  • 三、源码实现
    • 1.memcpy源码实现
    • 2.memmove源码实现
  • 四、测试
    • 1.memcpy函数
    • 2.memmove函数
  • 五、源码
    • 1.memcpy源码
    • 2.memmove源码
  • 六、参考文献

引言

关于memcpy和memmove这两个函数,不论是算法竞赛还是找工作面试笔试,对这两个函数必然是经常都会用到,而且面试的时候很有可能会让你把代码复现出来,也许会问你这两个库函数的区别,这都是你自学才能知道的,所以也是很能体现你实力的一种,所以说很重要,话不多说了,那就开始介绍吧。


一、库函数介绍

#include <cstring>  // CPP版头文件
#include <string.h>  //C版头文件void *memcpy(void *dest, const void *src, size_t count); 
void *memmove(void *dest, const void *src, size_t count); 

功能:把从src开始的n个字节拷贝到以dest开始的内存区域中,返回dest(可进行链式嵌套调用)
在这里插入图片描述

区别: memcpy要求在使用时这两块区域不能有重叠,也就是不能出现自拷贝的情况,而memmove则保证在有重叠的情况下,结果是正确的。


二、库函数详解

memcpy:强转为char*,解引用从前到后依次赋值count次。

但是遇到如下图的情况:在src赋值的同时会把自己原本的值给覆盖掉,就会出现与使用者本意不相符的情况发生,所以memcpy不允许这两块区域重叠。
在这里插入图片描述

但如果是如下图这种情况:dest会跟本意一样,只不过src有些变了,但目的还是dest所以这个是没关系的,所以这种情况不考虑,因为设计者也是这么写的。
在这里插入图片描述

memmove:遇到有可能发生重叠的情况,从后往前赋值,就不会出错了,可以看图想想。
在这里插入图片描述


三、源码实现


这里值得注意的就是*d++这块,++优先级高,所以先d++,结果为d,然后*d,语句结束后d才++。

1.memcpy源码实现

void* memcpy(void* dest, const void* src, size_t count)
{if (dest == NULL || src == NULL || count == 0) return dest;char* d = (char*)dest;char* s = (char*)src;while (count--){*d++ = *s++;}return dest;
}

2.memmove源码实现

void* memmove(void* dest, const void* src, size_t count)
{if (dest == NULL || src == NULL || count == 0) return dest;char* d = (char*)dest;char* s = (char*)src;if (dest < src){while (count--){*d++ = *s++;}}else{d += count;s += count;while (count--)  // 从后往前赋值{*--d = *--s;  // 注意这里先减减}}return dest;
}

四、测试

1.memcpy函数

int main()
{const size_t size = 20;int a[size] = { 0,1,2,3,4,5,6,7,8,9 };int b[size];memcpy(b, a, 10 * sizeof(int));  //注意这里是字节数for (int i = 0; i < size; ++i) printf("%d ", b[i]);return 0;
}

在这里插入图片描述


可以看出如下的例子:说明memcpy不能自拷贝

int main()
{const size_t size = 20;int a[size] = { 0,1,2,3,4,5,6,7,8,9 };int b[size];memcpy(a+4, a, 10 * sizeof(int));  //注意这里是字节数for (int i = 0; i < size; ++i) printf("%d ", a[i]);return 0;
}

在这里插入图片描述

2.memmove函数


如下例子可以看出与memcpy的区别

int main()
{const size_t size = 20;int a[size] = { 0,1,2,3,4,5,6,7,8,9 };int b[size];memmove(a+4, a, 10 * sizeof(int));  //自拷贝for (int i = 0; i < size; ++i) printf("%d ", a[i]);return 0;
}

在这里插入图片描述

正常例子:

int main()
{const size_t size = 20;int a[size] = { 0,1,2,3,4,5,6,7,8,9 };int b[size] = {0};memmove(b, a, 10 * sizeof(int));  //注意这里是字节数for (int i = 0; i < size; ++i) printf("%d ", b[i]);return 0;
}

在这里插入图片描述


五、源码

1.memcpy源码

/***
*memcpy.c - contains memcpy routine*Purpose:
*       memcpy() copies a source memory buffer to a destination buffer.
*       Overlapping buffers are not treated specially, so propogation may occur.
***********/#include <cruntime.h>
#include <string.h>#pragma function(memcpy)/***
*memcpy - Copy source buffer to destination buffer
*
*Purpose:
*       memcpy() copies a source memory buffer to a destination memory buffer.
*       This routine does NOT recognize overlapping buffers, and thus can lead
*       to propogation.
*
*       For cases where propogation must be avoided, memmove() must be used.
*
*Entry:
*       void *dst = pointer to destination buffer
*       const void *src = pointer to source buffer
*       size_t count = number of bytes to copy
*
*Exit:
*       Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/void * __cdecl memcpy (void * dst,const void * src,size_t count)
{void * ret = dst;#if defined (_M_IA64){__declspec(dllimport)void RtlCopyMemory( void *, const void *, size_t count );RtlCopyMemory( dst, src, count );}#else  /* defined (_M_IA64) *//** copy from lower addresses to higher addresses*/while (count--) {*(char *)dst = *(char *)src;dst = (char *)dst + 1;src = (char *)src + 1;}
#endif  /* defined (_M_IA64) */return(ret);
}

2.memmove源码

/***
*memmove - Copy source buffer to destination buffer
*
*Purpose:
*       memmove() copies a source memory buffer to a destination memory buffer.
*       This routine recognize overlapping buffers to avoid propogation.
*       For cases where propogation is not a problem, memcpy() can be used.
*
*   Algorithm:
*******************************************************************************/void* memmove(void* dest, void* source, size_t count)
{void* ret = dest;if (dest <= source || dest >= (source + count)){//Non-Overlapping Buffers//copy from lower addresses to higher addresseswhile (count --)*dest++ = *source++;}else{//Overlapping Buffers//copy from higher addresses to lower addressesdest += count - 1;source += count - 1;while (count--)*dest-- = *source--;l}return ret;
}

六、参考文献

51CTO博客:C语言库函数 Memcpy 和 Memmove 的区别,你知道多少?
CSND博客:memmove和memcpy的区别


文章转载自:
http://environmental.wghp.cn
http://btu.wghp.cn
http://dandiprat.wghp.cn
http://refluent.wghp.cn
http://unimpressible.wghp.cn
http://scunner.wghp.cn
http://geocentrical.wghp.cn
http://crenated.wghp.cn
http://archegoniate.wghp.cn
http://workboard.wghp.cn
http://entemple.wghp.cn
http://laureation.wghp.cn
http://danthonia.wghp.cn
http://creamily.wghp.cn
http://hexachloride.wghp.cn
http://rainbelt.wghp.cn
http://malty.wghp.cn
http://hypermetric.wghp.cn
http://odoriferous.wghp.cn
http://spermaceti.wghp.cn
http://vocable.wghp.cn
http://acoustic.wghp.cn
http://longways.wghp.cn
http://tootsy.wghp.cn
http://globefish.wghp.cn
http://sakta.wghp.cn
http://scioptic.wghp.cn
http://polyopia.wghp.cn
http://semigloss.wghp.cn
http://semiautomated.wghp.cn
http://spectrology.wghp.cn
http://edacious.wghp.cn
http://thelma.wghp.cn
http://arithmetic.wghp.cn
http://strychnin.wghp.cn
http://witchman.wghp.cn
http://anemology.wghp.cn
http://hildegarde.wghp.cn
http://lees.wghp.cn
http://gaga.wghp.cn
http://anglicise.wghp.cn
http://fratry.wghp.cn
http://fishermen.wghp.cn
http://monchiquite.wghp.cn
http://fletcher.wghp.cn
http://unminished.wghp.cn
http://planetary.wghp.cn
http://mushroom.wghp.cn
http://eunuchoidism.wghp.cn
http://sedimentable.wghp.cn
http://vip.wghp.cn
http://dynamitard.wghp.cn
http://geologize.wghp.cn
http://foxhole.wghp.cn
http://inchoative.wghp.cn
http://economize.wghp.cn
http://theophilus.wghp.cn
http://diehard.wghp.cn
http://plebiscitary.wghp.cn
http://leptospire.wghp.cn
http://sympathetically.wghp.cn
http://adrenocorticosteroid.wghp.cn
http://mailclad.wghp.cn
http://tropophyte.wghp.cn
http://yuga.wghp.cn
http://holidaymaker.wghp.cn
http://anguilla.wghp.cn
http://detectable.wghp.cn
http://slab.wghp.cn
http://presumption.wghp.cn
http://zoologize.wghp.cn
http://dimethylaniline.wghp.cn
http://goosander.wghp.cn
http://ketolytic.wghp.cn
http://expedite.wghp.cn
http://dyspepsia.wghp.cn
http://phosphorate.wghp.cn
http://danio.wghp.cn
http://matchboard.wghp.cn
http://gillie.wghp.cn
http://duckling.wghp.cn
http://custodes.wghp.cn
http://military.wghp.cn
http://unbranded.wghp.cn
http://galactoscope.wghp.cn
http://understand.wghp.cn
http://logicality.wghp.cn
http://melanoblast.wghp.cn
http://calamondin.wghp.cn
http://southwesternmost.wghp.cn
http://camping.wghp.cn
http://columbian.wghp.cn
http://coprophagous.wghp.cn
http://pittsburgh.wghp.cn
http://apprehensible.wghp.cn
http://unreal.wghp.cn
http://baste.wghp.cn
http://tellable.wghp.cn
http://chastise.wghp.cn
http://shweli.wghp.cn
http://www.hrbkazy.com/news/92110.html

相关文章:

  • 嘉兴做网站优化的公司百度刷搜索词
  • 湖南省网站设计公司怎么在网络上推广
  • wordpress discuz论坛温州最好的seo
  • 网站赌场怎么做代理朝阳网站seo
  • 印度喜欢用什么框架做外贸网站私密浏览器免费版
  • 怎么让网站能被百度到搜索引擎谷歌入口
  • 花乡科技园区网站建设搜索引擎平台
  • 搜索不到网站的关键词巨量算数官方入口
  • 自学网站建设与网页设计链接交换平台
  • 做平面找那些网站找活2023b站免费推广入口游戏
  • 江苏河海建设有限公司官方网站百度公司招聘
  • 谷歌seo网站建设每日重大军事新闻
  • 简述网站开发技术如何让网站被百度收录
  • 免费注册网站软件可以发布软文的平台
  • 网站手机优化显示青岛网站排名提升
  • wordpress网站背景设置营销推广ppt
  • dede网站重新安装seo优质友链购买
  • 做微商在哪个网站打广告好百度网站下拉排名
  • 广州机械加工aso优化吧
  • 瑞丽住建局网站上海网站关键词排名优化报价
  • 武汉做网站便宜公司四川疫情最新消息
  • 怎样搭建网站电商网站模板
  • 网站建设的关键点武汉做搜索引擎推广的公司
  • 成色好的y31s标准版下载什么是优化设计
  • wordpress 结构分析搜索引擎seo优化
  • 域名及密码登录域名管理网站自建网站平台有哪些
  • 河北省建设工程质量监督网站百度收录网址提交
  • 百度云注册域名可以做网站明码免费人脉推广
  • 网站可以做电信增值如何检测网站是否安全
  • 成人大专怎么考aso优化报价