The StrDup () function in C language and its difference from the strcpy () function _c language

Source: Internet
Author: User

Header file:

#include <string.h>

To define a function:

char * strdup (const char *s);

Function Description: StrDup () will first use MAOLLOC () to configure the same size as the parameter s string, and then copy the contents of the parameter S string to the memory address, and then return the address. The address can finally be released using free ().

Return value: Returns a string pointer to the copied new string address. Returning NULL indicates insufficient memory.

Example

#include <string.h>
Main () {
  char a[] = "StrDup";
  char *b;
  b = StrDup (a);
  printf ("b[]=\"%s\ "\ n", b);
}

Execution results:

b[]= "StrDup"

The difference between the StrDup () function and the strcpy () function
StrDup is not a standard C function, so Linux will complain! ~
strcpy is the standard C function, the error is in Windows because the pointer did not apply for space bar! ~
Can first strlen judge from the size, then to apply for space, and then strcpy will not be an error! ~

StrDup can copy the content to be copied directly to the uninitialized pointer, because it automatically allocates space to the destination pointer and uses the end
To manually release the system automatically allocated space
strcpy's destination pointer must be a pointer to the allocated memory

Recently looking at someone else to write the C language source code, many people like to use StrDup to copy strings, I think this habit is not good, because if you want to make your program transplant better,
Forget to have this function. The main reasons I deny it are:
1 with the StrDup function, often we will forget the release of memory, may be the reason for the C library function is not enough, after all, the other modules allocated memory, their own module release it.
2 on different platforms, we may use different methods for strdup memory allocation functions, such as in some C libraries with malloc, and in some C + + libraries, with new (because C + + libraries may rewrite the relevant C library code). So the user in the release of it when there is a lot of doubt, is the use of free or with delete[] to release the allocated memory?! If we are in charge of the assumption, free to release it, the operation is unknown. It may work properly, may be a partial memory leak, or it may be a program crash. The correctness of your own program depends on the compiler, very uncomfortable!

I think, in the module, unless the need to allocate their own memory needs of other modules to release, or should be sold, as far as possible to avoid the coupling between the modules, reduce the memory leakage factors.
Then the reader may ask if string copying is often used, similar to the following code

Char *dest = malloc (strlen (SRC) + 1);
ASSERT (dest!= NULL);
strcpy (dest, SRC);

Often want to be used, write 3 lines of code more verbose, then might as well use the macro to fix it. The benefit of this is to determine that the memory is allocated with malloc, the portability is much better, isn't it?! In addition, you define the macros, allocate memory to release, will not forget it

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.