memcpy函数用法和strcpy的区别

下次自动登录
现在的位置:
& 综合 & 正文
std::memset、std::memcpy和std::strcpy的区别
/kevinGaoblog/archive//2552796.html
Fill block of memory
void * memset ( void * ptr, int value, size_t num );
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
Parameters
Pointer to the block of memory to fill.
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
Number of bytes to be set to the value.
Return Value
ptr is returned.
#include &iostream&
#include &cstring&
int main ()
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
1 ------ every programmer should know memset!
Copy block of memory
void * memcpy ( void * destination, const void * source, size_t num );
Copies the values of num bytes from the location pointed by source directly to the memory block pointed bydestination.
The underlying type of the objects pointed by both the source and destination pointers are irreleva The result is a binary copy of the data.
The function does not check for any terminating null character in source - it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at leastnum bytes, and should not overlap (for overlapping memory blocks,
a safer approach).
Parameters
destination
Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
Pointer to the source of data to be copied, type-casted to a pointer of type void*.
Number of bytes to copy.
Return Value
destination is returned.
#include &iostream&
#include &cstring&
int main ()
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
cout&&"str1:"&&str1&&
cout&&"str2:"&&str2&&
cout&&"str3:"&&str3&&
1 str1: Sample string
2 str2: Sample string
3 str3: copy successful
Copy characters from string
char * strcpy ( char * destination, const char * source );
Copies the C string pointed by source into the array pointed by destination, including the terminating null character.To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string
as source (including the terminating null character), and should not overlap in memory with source.
Parameters
destination
Pointer to the destination array where the content is to be copied.
C string to be copied.
Return Value
destination is returned.
#include &iostream&
#include &cstring&
int main ()
char str1[]= "Sample string";
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,"copy successful");
cout&&"str1:"&&str1&&
cout&&"str2:"&&str2&&
cout&&"str3:"&&str3&&
1 str1: Sample string
2 str2: Sample string
3 str3: copy successful
**************************************************************
&&&&推荐文章:
【上篇】【下篇】温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1839)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'sprintf、strcpy 及 memcpy 函数的区别',
blogAbstract:'sprintf 可以进行额外的格式化\r\nstrcpy 会复制直到出现 \'\' 为止,可能溢出\r\nstrncpy 会复制一个以 \'\' 结束的字符串,但是如果字符串长度超过指定数量则被截断,但结果可能不包含 \'\' 表示结束\r\nmemcpy 只负责复制指定数量的 bytes,不处理 \'\' 的情况\r\nmemmove 在 memcpy 的基础上对 overlap 的情况进行了处理。这些函数的区别在于 实现功能 以及 操作对象\r\n 不同。\r\n\r\nstrcpy 函数操作的对象是 字符串,完成 从 \r\n源字符串 到 目的字符串 的 拷贝 功能。',
blogTag:'',
blogUrl:'blog/static/7',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/7',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}&&&&&&&&&&&&
说出来让大家高兴高兴 · 245人参与
准备工作可以做起来了 · 4597人参与
豆友心中有杆秤 · 2267人参与
江山代有新词出,各领风骚数百天 · 2549人参与
秋天散步才是正经事 · 6978人参与
天朗气清,烹一炉秋天 · 11150人参与
&&&&&&&&&&&&
海地sealand的日记
秋风起,秋叶落。 正所谓一叶知秋。 走在路上,常常会被随风拂落到叶子吸引了视线...
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&
&&&&&&&&&&&&
15302 个成员
44379 个成员
22138 个成员
21923 个成员
10383 个成员
109563 个成员
93277 个成员
91511 个成员
31701 个成员
3830 个成员
15478 个成员
&&&&&&&&&&&&
〔英〕蕾秋·乔...
〔英〕菲利普·...
〔美〕裘帕·拉...
原创数字作品
&&&&&&&&&&&&
〔加〕王平常
〔意〕安东妮娅·波齐(Antonia Pozzi)
Alex Lickerman
流派: 流行 Pop
流派: 民谣 Folk
6947人关注
流派: 摇滚 Rock
流派: 民谣 Folk
流派: 摇滚 Rock
43039人关注
豆瓣新碟榜
&&&&&&&&&&&&
&&&&&&&&&&&&
一路西行一路唱
冷门小清新
不曾忘记的动漫原声
配着做图消消气
ERICD原创设计以传统服装制作工艺为主导,结合当下最流行的...
每周五定时上新,注意关注偶
不用装修,NaNaZ home 也能你家变得更美一些。
卖我会穿的打底,做你会爱的姑娘
······
无锡 · 本周热门活动
&&&&&&&&&&&&
寅派动力新文化空间 梁溪区...
无锡大剧院 小剧场 江苏省...
无锡市新工人文化宫 太湖新...
无锡演艺剧院 解放南路558...
& 2005-, all rights reserved 北京豆网科技有限公司
京ICP备号 网络视听许可证号
京网文[8号
&&新出网证(京)字129号
违法和不良信息举报电话:&
电话:123775807人阅读
原型声明:extern char *strcpy(char* dest, const char *src);
头文件:#include &&
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的。
#include&iostream.h&;
char*strcpy(char*
strDest,constchar*
strDestCopy=strD
&&if((strDest==NULL)||(strSrc==NULL))
&&&throw&Invalid
argument&;
&&while((*strDest++=*strSrc++)!='\0');
&&returnstrDestC
voidmain()
&&chara[20],c[]=&i
am teacher!&;
&&&strcpy(a,c);
&&catch(char*
&&&cout&&strInfo&&
&&&exit(-1);
&&cout&&a&&
void *memcpy(void *dest, const void *src, size_t n);
功能:从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中
返回 值:返回dest的值
一个内存拷贝函数的实现体
void *memcpy(void *pvTo,const void *pvFrom,size_t size)
&assert((pvTo!=NULL)&&(pvFrom!=NULL));
&byte *pbTo=(byte*)pvTo; //防止地址被改变
&byte *pbFrom=(byte*)pvF
&while (size-- &0)
&pbTo++ = pbForm++;
&return pvTo;
作用:将s中的字符串复制到字符d中。
&string.h&
&&&char*s=&Golden
Global View&;
&&&chard[20];
&&&clrscr();
&&&memcpy(d,s,(strlen(s)+1));
&&&printf(&%s&,d);
&&&getchar();
&&&return0;
输出结果:Golden Global View
作用:将s中第14个字符开始的4个连续字符复制到d中。(从0开始)
&string.h&
&&&char*s=&Golden
Global View&;
&&&chard[20];
&&&memcpy(d,s+14,4);
&&&d[4]='\0';
&&&printf(&%s&,d);
&&&getchar();
&&&return0;
输出结果: View
作用:复制后覆盖原有部分数据
&string.h&
intmain(void)
&&&charsrc[]
= &******************************&;
&&&chardest[]
= &abcdefghijlkmnopqrstuvwxyz0123as6&;
&&&printf(&destination
before memcpy: %s\n&,
&&&memcpy(dest,
src, strlen(src));
&&&printf(&destination
after memcpy: %s\n&,
&&&return0;
输出结果:
destination before memcpy:abcdefghijlkmnopqrstuvwxyz0123as6
destination after memcpy: ******************************as6
strcpy与memcpy的区别:
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符&\0&才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:244091次
积分:2190
积分:2190
排名:第18382名
原创:50篇
转载:23篇
评论:32条
(2)(1)(4)(4)(1)(7)(2)(1)(2)(1)(1)(1)(1)(2)(4)(2)(10)(5)(9)(2)(5)(6)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 memcpy和strcpy效率 的文章

 

随机推荐