Strdup function usage

Source: Internet
Author: User

Strdup function usage

 

Function Name: strdup

Function: copy the string to the new position.

Usage: char * strdup (char * Str );

 

This function is explained in the Linux man manual as follows:

The strdup () function returns a pointer Toa new string which is

Duplicate of the string S. Memory for thenew string is obtained

Malloc (3), and can be freed with free (3 ).

The strndup () function is similar, but onlycopies at most N charac-

TERs. If S is longer than N, only ncharacters are copied, and a Termi-

Nating NUL is added.

 

Strdup function prototype:

Strdup () is mainly used to copy a copy of string s, which is returned by the function return value. This copy has its own memory space and is irrelevant to S. After the strdup function is used to copy a string, remember to delete the dynamically applied memory in the function. The strdup function parameter cannot be null. If it is null, a segment error is reported, this function includes the strlen function, and the function parameter cannot be null.

How strdup works:

Char * _ strdup (const char * s)

{

Size_t Len = strlen (s) + 1;

Void * New = malloc (LEN );

If (New = NULL)

Return NULL;

Return (char *) memcpy (new, S, Len );

}

 

Instance 1:

C/C ++ code

# Include <stdio. h>

# Include <string. h>

# Include <alloc. h>

Int main (void)

{

Char * dup_str, * string = "ABCDE ";

Dup_str = strdup (string );

Printf ("% s \ n", dup_str); free (dup_str); Return 0;

}

 

Instance 2:

# Include <stdio. h>

# Include <stdlib. h>

# Include <string. h>

Unsigned int test ()

{

Charbuf [] = "Hello, world! ";

Char * pb = strndup (BUF, strlen (BUF ));

Return (unsignedint) (PB );

}

 

Int main ()

{

Unsigned int PCH = test ();

Printf ("testing: % s \ n", (char *) PCH );

Free (void *) PCH );

Return 0;

}

Use strndup in the test function, and the test function can still operate the memory and release it.

The problem extended from this problem is how to make the memory data obtained by the function outgoing but still available.

Currently, I only think of two solutions: one external variable, such as passing a memory block pointer to the function. However, you have to pass enough memory, that is, you cannot know the buffer size required by this function in advance.

Another method is to apply for static variables in the function, which is also a global variable. However, the disadvantage of this method is that when the function is run multiple times, the data in the static variable is overwritten. Another method of this type is to use global variables, but this is quite the same as using static variables. The difference is that global variables can be operated and controlled, while static variables do not output them to functions, it cannot be controlled. Another method is described above, which is implemented using the memory in the heap, but there is a risk. Strdup allocates space from the heap! Strdup calls malloc, so it needs to be released! For stacks: stacks are managed by programmers. For example, new and malloc are allocated on stacks!

Stack is managed by the compiler.

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.