c语⾔strdup函数_在CC++中使⽤strdup()函数的指南c语⾔strdup函数
In this article, we’ll take a look at using the strdup() function in C/C++.
在本⽂中,我们将研究在C / C ++中使⽤strdup()函数。
The strdup() function is very useful if you want to duplicate the contents of a string onto another string.
如果要将⼀个字符串的内容复制到另⼀个字符串上,strdup()函数将⾮常有⽤。
Let’s see how we can utilize this function, using some simple examples.
通过⼀些简单的⽰例,让我们看看如何利⽤此功能。
C / C ++中strdup()函数的基本语法 (Basic Syntax of the strdup() function in C/C++)
This takes in an input char* string, and duplicates it into another char* string. It returns the pointer to this string.
这将接受⼀个输⼊的char*字符串,并将其复制到另⼀个char*字符串中。 它返回指向该字符串的指针。
This is also defined inside the header file <string.h>, so we must include it first.
这也是在头⽂件<string.h>定义的,因此我们必须⾸先包含它。
Therefore, the function prototype is as follows:
因此,函数原型如下:
#include <string.h>
char* strdup(const char* src);
Since we will not be modifying the input string, we pass it as const char*!
由于我们不会修改输⼊字符串,因此将其作为const char*传递。
NOTE: This function is NOT defined in the ISO C99 standards, but it is defined in POSIX systems. If you’re unsure about using this function, refer to the below section for a sample implementation.
注意 :此功能未在ISO C99标准中定义,但已在POSIX系统中定义。 如果不确定使⽤此功能,请参考以下部分的⽰例实现。
Let’s now look at a simple example, which shows this function coming into play.
现在让我们看⼀个简单的⽰例,该⽰例说明了该功能的作⽤。
使⽤strdup()–⼀个简单的例⼦ (Using strdup() – A simple Example)
We’ll take an input string, and copy it into another string.
我们将输⼊⼀个字符串,并将其复制到另⼀个字符串中。
int main() {
char inp[] = "Hello from JournalDev!";
char* output = strdup(inp);
printf("Input: %s\n", inp);
printf("Output: %s\n", output);
return 0;c++中string的用法
}
Output
输出量
Input: Hello from JournalDev!
Output: Hello from JournalDev!
As you can see, we do indeed get our output to be the same string as the input.
如您所见,我们确实的确将输出与输⼊的字符串相同。
使⽤strdup()与使⽤strcpy() (Using strdup() vs Using strcpy())
You may very well wonder – if strdup() duplicates an input string, and strcpy() copies from an input string, what is the difference?
您可能会很好奇–如果strdup()复制输⼊字符串,⽽strcpy()从输⼊字符串复制,有什么区别?
The difference between the two lies in how these two functions are implemented.
两者之间的区别在于这两个功能的实现⽅式。
When you use strcpy(dst, src), you are literally copying from the source string to the destination string. Here, you are responsible for both allocating and de-allocating the memory with dst.
使⽤strcpy(dst, src) ,实际上是从源字符串复制到⽬标字符串。 在这⾥,您负责使⽤dst分配和取消分配内存。
This is because strcpy simply copies data. Here is an elegant one-line implementation from StackOverflow answer!
这是因为strcpy只是复制数据。 这是 StackOverflow答案中的⼀种优雅的单⾏实现!
void strcpy(char* dst, char* src) {
while (*dst++ = *src++);
}
What does this imply? This means that you can use strcpy() on both static (associated with stack) memory as well as dynamic (associated with heap) memory.
这意味着什么? 这意味着您可以在静态(与堆栈相关联)内存以及动态(与堆相关联)内存上使⽤strcpy() 。
int main() {
char a[] = "Hello";
// Memory associated with stack
char b[100];
strcpy(b, a);
// Memory associated with heap
char* c = (char*) malloc (10 * sizeof(char));
strcpy(b, c);
printf("a = %s\nb=%s\nc=%s\n", a, b);
free(c);
return 0;
}
Output
输出量
a = Hello
b = Hello
c = Hello
Indeed, we could copy to both static as well as dynamic memory locations, using strcpy().
确实,我们可以使⽤strcpy()复制到静态和动态内存位置。
Now, strdup() uses malloc() under the hood to automatically allocate the memory for you. However, this means that you must free the memory yourself, after you finish using it!
现在, strdup()在strdup()使⽤malloc()为您⾃动分配内存。 但是,这意味着您必须在使⽤完内存后⾃⼰释放内存!
So, simply put, strdup() allocates memory using malloc(), and then copies your source string to the allocated memory.
因此,简单地说, strdup()使⽤malloc()strdup()分配内存,然后将源字符串复制到分配的内存中。
strdup()的⽰例实现 (A Sample Implementation of strdup())
Here is an efficient implementation, using memcpy() to speed things up, as compared to naive copying using strcpy().
与使⽤strcpy()进⾏天真复制相⽐,这是⼀种⾼效的实现,使⽤memcpy()可以加快处理速度。
char* my_strdup(char* input) {
// We need strlen(src) + 1, since we have to account for '\0'
int len = strlen(input) + 1;
char* output = (char*) malloc ((len + 1) * sizeof(char));
if (output == NULL) return NULL;
output = (char*) memcpy(output, input, len);
return output;
}
Now, let’s look at a complete program, to verify that we can use this properly. Remember, we must also free the memory returned by malloc()!
现在,让我们看⼀个完整的程序,以验证我们可以正确使⽤它。 记住,我们还必须释放malloc()返回的malloc() !
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* my_strdup(char* input) {
// We need strlen(src) + 1, since we have to account for '\0'
int len = strlen(input) + 1;
char* output = (char*) malloc ((len + 1) * sizeof(char));
if (output == NULL) return NULL;
output = (char*) memcpy(output, input, len);
return output;
}
int main() {
char inp[] = "Hello from JournalDev!";
char* output = my_strdup(inp);
printf("Input: %s\n", inp);
printf("Output: %s\n", output);
// Free the memory address returned using malloc()
free(output);
return 0;
}
Output
输出量
Input: Hello from JournalDev!
Output: Hello from JournalDev!
Indeed, we did get the same output as before! And we don’t seem to have any memory leaks, since we freed the corresponding memory at the end.
确实,我们确实获得了与以前相同的输出! ⽽且我们似乎没有任何内存泄漏,因为我们在最后释放了相应的内存。
结论 (Conclusion)
We learned about we could use the strdup() function in C/C++. We also learned about the differences between strdup() and strcpy(), so use them wisely!
我们了解到可以在C / C ++中使⽤strdup()函数。 我们还了解了strdup()和strcpy()之间的区别,因此请明智地使⽤它们!
For similar content, do check out our on C programming!
对于类似的内容,请查看我们有关C编程的 !
参考资料 (References)
on strdup()
关于strdup()的
on strcpy() vs strdup()
关于strcpy()vs strdup()的
c语⾔strdup函数

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。