Strdup of function in C and its difference from strcpy of function

  • 2020-04-02 03:17:08
  • OfStack

The header file:


#include <string.h>

Definition function:


char * strdup(const char *s);

Strdup () USES maolloc() to configure the same size as the s string, then copies the contents of the s string to the memory address and returns the address. The address can finally be freed using free().

Return value: returns a string pointer to the copied new string address. If it returns NULL, it is out of memory.

sample


#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 reports an error! ~
Strcpy is a standard c function, error in Windows because the pointer does not apply space bar! ~
Strlen can determine the size of from, then request space for to, and then strcpy won't report any errors! ~

Strdup can directly copy the contents to be copied to the uninitialized pointer, because it automatically allocates space to the destination pointer and ends up using
Manually release the space automatically allocated by the system
The destination pointer for strcpy must be a pointer that has allocated memory

Recently, I have been looking at the c source code written by others. Many people like to use strdup to copy strings, which I think is a bad habit, because if you want to make your program more portable,
Let's forget about this function. The main reason I reject it is:
1) when using strdup function, we tend to forget the memory release. The possible reason is that we don't know enough about the C library function. After all, other modules allocate memory and their own modules release it.
2) on different platforms, we may take different approaches to the function of strdup memory allocation, such as malloc in some c libraries and new in some c++ libraries (because the c++ library may rewrite the relevant c library code). So there was a lot of confusion when the user released it, should he use free or delete[] to free the allocated memory? ! If our supervisor assumes, use free to release it, operation unknown. It could work, it could be a partial memory leak, it could be a program crash. The correctness of your program depends on the compiler.

In my opinion, in a module, unless the memory allocated by itself needs to be released by other modules, it should be produced and sold by itself, avoiding such coupling between modules as much as possible, and reducing the memory leak factor.
So the reader might ask, if string copying is used a lot, something like this


char *dest = malloc( strlen( src ) + 1 );
assert( dest != NULL );
strcpy( dest, src );

Often used, writing 3 lines of code is verbose, so use macros to handle it. The nice thing about this is that it makes sure that the memory is malloc allocated, so it's much more portable, isn't it? ! In addition, the definition of their own macros, after allocating memory to release, never forget it


Related articles: