hi,
can anyone explain me what strcpy() & memcpy() actually does?plzz reply me soon......
Printable View
hi,
can anyone explain me what strcpy() & memcpy() actually does?plzz reply me soon......
Functionally there's no difference: both copy memory from a location to another...
The main difference is the function declaration... probably also implementation differs, and memcpy should reasonably faster than strcpy, because strcpy must stop when encounter the first '\0' character, so it must test char by char (or something similar), while memcpy can optimize using memory alignement and other stuffs...
Hi,In Chinese:
strcpy包含两个参数,无法确定要拷贝的字节数,而只能通过识别'\0'来完成一次拷贝。
memcpy的函数中包含要拷贝的字节数,80x86下是用rep movsb实现的,拷贝的速度相对要快些。
Huh? One copies a block of memory, and the other copies a sequence of bytes, terminating on a NULL character. Quite a big difference, I'd have thought, if you're writing code that has to work.Quote:
Functionally there's no difference
while using memcpy be careful since u have to check the destination size and source size which ever is greater or smaller and according to that do the memcpy and insert the '\0' character at the end after the memcpy when copying strings
I just got an example of what i had done previously
Its something like this
memcpy(dest, src,sizeof(dst));
if(strlen(src)>sizeof(dest))
{
dest[sizeof(dest)-1]='\0';
}
else
{
dest [strlen(src)]='\0';
}
same thing can be applicable for memmove but memory areas overlap in that