C语言 strcpy 与 strdup
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14020380/
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
strcpy vs strdup
提问by johan
I read that strcpyis for copying a string, and strdupreturns a pointer to a new string to duplicate the string.
我读到它strcpy是为了复制一个字符串,并strdup返回一个指向新字符串的指针以复制该字符串。
Could you please explain what cases do you prefer to use strcpyand what cases do you prefer to use strdup?
您能否解释一下您更喜欢使用strcpy哪些案例以及您更喜欢使用哪些案例strdup?
回答by Abdul Muheedh
strcpy(ptr2, ptr1)is equivalent to while(*ptr2++ = *ptr1++)
strcpy(ptr2, ptr1)相当于 while(*ptr2++ = *ptr1++)
where as strdup is equivalent to
其中 strdup 相当于
ptr2 = malloc(strlen(ptr1)+1);
strcpy(ptr2,ptr1);
(memcpy versionmight be more efficient)
(memcpy 版本可能更有效)
So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough.
因此,如果您希望复制的字符串在另一个函数中使用(因为它是在堆部分中创建的),则可以使用 strdup,否则 strcpy 就足够了。
回答by Kerrek SB
The functions strcpyand strncpyare part of the C standard library and operate on existing memory. That is, youmust provide the memory into which the functions copy the string data, and as a corollary, youmust have your own means of finding out how much memory you need.
函数strcpy和strncpy是 C 标准库的一部分,并在现有内存上运行。也就是说,您必须提供函数将字符串数据复制到其中的内存,并且作为必然结果,您必须有自己的方法来确定您需要多少内存。
By constrast, strdupis a Posix function, and it performs dynamic memory allocation for you. It returns a pointer to newly allocated memory into which it has copied the string. But youare now responsible for this memory and must eventually freeit.
相比之下,它strdup是一个 Posix 函数,它为您执行动态内存分配。它返回一个指向新分配的内存的指针,它已将字符串复制到该内存中。但是你现在要对这个记忆负责,而且最终必须要负责free。
That makes strdupone of the "hidden malloc" convenience functions, and that's presumably also why it is not part of the standard library. As long as you use the standard library, you know that you must call one freefor every malloc/calloc. But functions such as strdupintroduce a hidden malloc, and you must treat it the same as a mallocfor the purpose of memory management. (Another such hidden allocation functions is GCC's abi::__cxa_demangle().) Beware!
这构成strdup了“隐藏malloc”的便利功能之一,这大概也是它不是标准库的一部分的原因。只要您使用标准库,您就知道必须free为每个malloc/调用一个calloc。但是诸如strduphidden 之类的函数引入了 hidden malloc,malloc出于内存管理的目的,您必须将其与 a 一样对待。(另一个这样的隐藏分配函数是 GCC 的abi::__cxa_demangle()。)当心!
回答by Oren
strdupallocates memory for the new string on the heap, while using strcpy(or its safer strncpyvarient) I can copy a string to a pre allocated memory on eitherthe heap or the stack.
strdup分配存储器,用于在堆上新的字符串,同时使用strcpy(或它的更安全strncpy的变体光盘),我可以一个字符串复制到一个预先分配的存储器上或者堆或堆。
回答by Heikki Salokanto
In the accepted answer, the implementation of strdupis presented as:
在接受的答案中, 的实现strdup表示为:
ptr2 = malloc(strlen(ptr1)+1);
strcpy(ptr2,ptr1);
However, that is somewhat sub-optimal because both strlenand strcpyneed to find the length of the string by checking if each character is a \0.
然而,有些次优的,因为两者strlen并strcpy需要通过检查每个字符是一个查找的字符串的长度\0。
Using memcpyshould be more efficient:
使用memcpy应该更有效:
char *strdup(const char *src) {
size_t len = strlen(src) + 1;
char *s = malloc(len);
if (s == NULL)
return NULL;
return (char *)memcpy(s, src, len);
}
回答by Bharath Ravindra
char *strdup(char *pszSrch);
char *strdup(char *pszSrch);
strdupwill allocate storage the size of the original string. If storage allocation is successful, the original string is copied to the duplicate string.
strdup将分配存储原始字符串的大小。如果存储分配成功,则将原始字符串复制到重复字符串。
strdupd return NULLon failure. If memory is not allocated, copy fails strdupreturn NULL.
strdupdNULL失败返回。如果没有分配内存,复制失败strdup返回NULL。

