When is it a good idea to use strdup (vs malloc / strcpy)

Can I use malloc and strcpy to replace it? Which one is better?

e.g.:

char *s = "Global View";
char *d;
d = strdup(s);
free(d);

or

char *s = "Global View";
char *d = malloc(strlen(s) +1);
strcpy(d,s);
free(d);

Which one is better?

strdup(s); itself does not create a problem when allocation failures (calling code still needs to handle a NULL return), unlike the below which is undefined behavior or UB.

char *d = malloc(strlen(s) +1);
strcpy(d,s); // should not be called if `d == NULL`.

A typical implementation of strdup(s) does not walk the length of s twice like the alternate might.

// 1st pass to find length of `s`
char *d = malloc(strlen(s) +1);
// Weak compiler/library may run 2nd pass to find length of `s` and then copy
strcpy(d,s);

A good strdup(s) will make one pass and use optimal copy code when the length warrants it. Perhaps by using memcpy() or equivalent.

The key is that strdup() is expected to be used often and a library that implements this non-standard C library function is expected to be crafted to perform optimally. Use the best tool when it is available. Sample implementation:

#include <errno.h>
#include <stdlib.h>

char *strdup(const char *s) {
  if (s == NULL) { // Optional test, s should point to a string
    #ifdef EINVAL
      errno = EINVAL;  // For systems that support this "invalid argument" errno
    #ednif
    return NULL;  
  }
  size_t siz = strlen(s) + 1;
  char *y = malloc(siz);
  if (y != NULL) {
    memcpy(y, s, siz);
  } else {
    #ifdef ENOMEM
      errno = ENOMEM;  // For systems that support this "out-of-memory" errno
    #else
      ;
    #endif
  }
  return y;
}

Rolling your own strdup() does collide with reserved name space @Jonathan Leffler @Joshua

An important advantage to malloc()/memcpy()/strcpy() is that they are standard C library functions. strdup() is not in the standard C library, although it is very commonly implemented.

[edit] strdup() maybe in C2x: Add strdup and strndup to C2X?


There is not much difference other than strdup is shorted. strdup == malloc + strcpy


Use strdup() to be consistent in your use of libc string handling functions. strdup() implies the operand is libc's model of a null-terminated string.

libc's str...() functions flawlessly address the basics of C string handling, so use them whenever they'll suffice, if for no other reason than to make your code quicker to understand by others, and to avoid writing more code than necessary.

I personally wouldn't mix models without a compelling reason. Situations may arise where it is helpful or necessary to supplement libc string functions with custom functions, or maybe bypass them entirely, for example, not all C platforms provide libc. Maybe there are linking issues, or your working in a kernel context and can't access libc without crashing or a lot of effort, etc...

It can be tempting by using 0 or NULL to indicate the literal value '\0'. Most C programmers know all forms of NULL will work. The advantage of using '\0' where it's relevant is that it is a succinct way to disambiguate your intention. '\0' represents a character and nothing else.