首页 > 编程知识 正文

c语言strdup函数在C C 中使用strdup函数的指南,mast语言使用指南

时间:2023-05-03 13:56:35 阅读:258610 作者:2942

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.

我们将输入一个字符串,并将其复制到另一个字符串中。

#include <stdio.h>#include <string.h>int main() { char inp[] = "Hello from JournalDev!"; char* output = strdup(inp); printf("Input: %sn", inp); printf("Output: %sn", output); return 0;}

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 this 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() 。

#include <stdio.h>#include <string.h>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 = %snb=%snc=%sn", a, b); free(c); return 0;}

Output

输出量

a = Hellob = Helloc = 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 '' 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 '' 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: %sn", inp); printf("Output: %sn", 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 tutorial section on C programming!

对于类似的内容,请查看我们有关C编程的教程部分 !

参考资料 (References) cppreference.com page on strdup()

关于strdup()的cppreference.com页面 StackOverflow Question on strcpy() vs strdup()

关于strcpy()vs strdup()的StackOverflow问题



翻译自: https://www.journaldev.com/40458/strdup-c-plus-plus

c语言strdup函数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。