Analysis of strdup function of Linux C function

Source: Internet
Author: User

I. Function Analysis

1. Function Prototypes:

#include <string.h>

2. Function:

The StrDup () function is primarily a copy of the copy string s, returned by the function return value, which has its own memory space and is not associated with S. StrDup function copies a string, after use, to use the delete function to delete the dynamic request memory in the function, the parameters of the StrDup function cannot be null, and once null, a segment error is reported because the function includes the Strlen function, and the function argument cannot be null.

3.strdup function implementation

char * __strdup (const char *s)
{
size_t len = strlen (s) +1;
void *new = malloc (len);
if (new = = NULL)
return NULL;
Return (char *) memecpy (New,s,len);
}
#strdup只是内部分配了空间, and not released, the release needs to be done by the caller.

4. Function instances

#include <syslib.h>
#include <string.h>
int main (void)
{
Char *SRC = "This is the Jibo";
Char *dest=null;
Dest = StrDup (s);
printf ("The Dest%s\n", dest);
if (dest) free (dest);
return 0;
}
two. The difference between strdup and strcpy functions

1. Common denominator:

All two functions implement a copy of the string.

2. Different points:

1) strcpy function: Copy a string starting from the SRC address with a null terminator to the address space starting with dest

2) Realization:

Char *strcpy (char *strdest,const char *strsrc)
{
ASSERT ((strdest!=null) && (strsrc!=null));
char *address = strdest;
while ((*strdest + + = *strstrc++)! = ' + ');
return address;
}

3) realized by strcpy and StrDup functions

The 1>strdup function returns a pointer to the copied string, and the required space is allocated by the malloc () function and can be freed by the free () function. Stdrup can simply copy the content to be copied to a pointer that is not initialized because it automatically allocates space to the destination pointer.

The target pointer of the 2>strcpy must be a memory pointer already allocated.

4) Disadvantages of StrDup:

When you use the StrDup function, you tend to forget the memory release, because the action to request memory space is implemented within the StrDup function, and if the implementation of the function is not well understood, you forget to use the free function to release space.

This article is from the "Linux_woniu" blog, make sure to keep this source http://llu1314.blog.51cto.com/5925801/1965246

Analysis of strdup function of Linux C function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.