C语言 strcpy 与 memcpy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2898364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:30:38  来源:igfitidea点击:

strcpy vs. memcpy

cmemcpystrcpy

提问by Sachin Chourasiya

What is the difference between memcpy()and strcpy()? I tried to find it with the help of a program but both are giving the same output.

memcpy()和 和有strcpy()什么区别?我试图在程序的帮助下找到它,但两者都给出了相同的输出。

int main()
{
    char s[5]={'s','a','
sachin p is [sa], t is [sa]
','c','h'}; char p[5]; char t[5]; strcpy(p,s); memcpy(t,s,5); printf("sachin p is [%s], t is [%s]",p,t); return 0; }

Output

输出

void dump5(char *str);

int main()
{
    char s[5]={'s','a','
73 61 00 63 68  sa ch
73 61 00 00 00  sa
','c','h'}; char membuff[5]; char strbuff[5]; memset(membuff, 0, 5); // init both buffers to nulls memset(strbuff, 0, 5); strcpy(strbuff,s); memcpy(membuff,s,5); dump5(membuff); // show what happened dump5(strbuff); return 0; } void dump5(char *str) { char *p = str; for (int n = 0; n < 5; ++n) { printf("%2.2x ", *p); ++p; } printf("\t"); p = str; for (int n = 0; n < 5; ++n) { printf("%c", *p ? *p : ' '); ++p; } printf("\n", str); }

回答by egrunin

what could be done to see this effect

怎样做才能看到这种效果

Compile and run this code:

编译并运行此代码:

while((*dst++) = (*src++));

It will produce this output:

它将产生以下输出:

#include<stdio.h>
#include<string.h>

int main()
{
    char s[5]={'s','a','##代码##','c','h'};
    char p[5];
    char t[5];
    int i;
    strcpy(p,s);
    memcpy(t,s,5);
    for(i=0;i<5;i++)
        printf("%c",p[i]);
        printf("\n");
    for(i=0;i<5;i++)
        printf("%c",t[i]);

    return 0;
}

You can see that the "ch" was copied by memcpy(), but not strcpy().

您可以看到“ch”是由 复制的memcpy(),但不是由 复制的strcpy()

回答by Yann Ramin

strcpystops when it encounters a NUL ('\0') character, memcpydoes not. You do not see the effect here, as %sin printf also stops at NUL.

strcpy遇到 NUL ( '\0') 字符时停止,memcpy不会。您在这里看不到效果,因为%s在 printf 中也会在 NUL 处停止。

回答by fbrereto

strcpyterminates when the source string's null terminator is found. memcpyrequires a size parameter be passed. In the case you presented the printfstatement is halting after the null terminator is found for both character arrays, however you will find t[3]and t[4]have copied data in them as well.

strcpy当找到源字符串的空终止符时终止。memcpy需要传递一个大小参数。在您提供的情况下,printf语句在为两个字符数组找到空终止符后停止,但是您也会在其中找到t[3]t[4]复制数据。

回答by Viswesn

strcpycopies character from source to destination one by one until it find NULL or '\0' character in the source.

strcpy将字符从源一一复制到目标,直到在源中找到 NULL 或 '\0' 字符。

##代码##

where as memcpycopies data (not character) from source to destination of given size n, irrespective of data in source.

其中 as 将memcpy数据(非字符)从源复制到给定大小 n 的目标,而不管源中的数据如何。

memcpyshould be used if you know well that source contain other than character. for encrypted data or binary data, memcpy is ideal way to go.

memcpy如果您很清楚源包含字符以外的内容,则应使用。对于加密数据或二进制数据,memcpy 是理想的选择。

strcpyis deprecated, so use strncpy.

strcpy已弃用,因此请使用strncpy.

回答by Thomas Jones-Low

Because of the null character in your sstring, the printfwon't show anything beyond that. The difference between pand twill be in characters 4 and 5. pwon't have any (they'll be garbage) and twill have the 'c'and 'h'.

由于您的s字符串中有空字符,因此printf不会显示除此之外的任何内容。p和之间的区别t将在字符 4 和 5 中。p不会有任何(它们将是垃圾)并且t会有'c''h'

回答by Jeremy Friesner

The main difference is that memcpy()always copies the exact number of bytes you specify; strcpy(), on the other hand, will copy until it reads a NUL (aka 0) byte, and then stop after that.

主要区别在于memcpy()始终复制您指定的确切字节数;strcpy(),另一方面,将复制直到它读取一个 NUL(又名 0)字节,然后在此之后停止。

回答by euccas

  • Behavior difference: strcpystops when it encounters a NULLor '\0'
  • Performance difference: memcpyis usually more efficient than strcpy, which always scan the data it copies
  • 行为差异:strcpy遇到NULL或停止'\0'
  • 性能差异:memcpy通常比 效率更高strcpy,它总是扫描它复制的数据

回答by Jaspar L.

The problem with your test-program is, that the printf()stops inserting the argument into %s, when it encounters a null-termination \0. So in your output you probably have not noticed, that memcpy()copied the characters cand has well.

您的测试程序的问题是,当遇到空终止时,它会printf()停止将参数插入到 中。因此,在你的输出,你可能没有注意到,那复制的字符,并为好。%s\0memcpy()ch

I have seen in GNU glibc-2.24, that (for x86) strcpy()just calls memcpy(dest, src, strlen(src) + 1).

我在 GNU 中看到glibc-2.24,(对于 x86)strcpy()只是调用memcpy(dest, src, strlen(src) + 1).

回答by abhijeet keshri

printf("%s",...)stops printing the data when null is encountered, so both the outputs are same.

printf("%s",...)遇到 null 时停止打印数据,因此两个输出相同。

The following code differentiates between strcpyand memcpy:

以下代码区分strcpymemcpy

##代码##