strcpy vs strdup

CStrcpyStrdup

C Problem Overview


I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string.

Could you please explain what cases do you prefer to use strcpy and what cases do you prefer to use strdup?

C Solutions


Solution 1 - C

strcpy(ptr2, ptr1) is equivalent to while(*ptr2++ = *ptr1++)

where as strdup is equivalent to

ptr2 = malloc(strlen(ptr1)+1);
strcpy(ptr2,ptr1);

(memcpy version might be more efficient)

So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough.

Solution 2 - C

The functions strcpy and strncpy are part of the C standard library and operate on existing memory. That is, you must provide the memory into which the functions copy the string data, and as a corollary, you must have your own means of finding out how much memory you need.

By constrast, strdup is a Posix function, and it performs dynamic memory allocation for you. It returns a pointer to newly allocated memory into which it has copied the string. But you are now responsible for this memory and must eventually free it.

That makes strdup one of the "hidden malloc" convenience functions, and that's presumably also why it is not part of the standard library. As long as you use the standard library, you know that you must call one free for every malloc/calloc. But functions such as strdup introduce a hidden malloc, and you must treat it the same as a malloc for the purpose of memory management. (Another such hidden allocation functions is GCC's abi::__cxa_demangle().) Beware!

Solution 3 - C

strdup allocates memory for the new string on the heap, while using strcpy (or its safer strncpy varient) I can copy a string to a pre allocated memory on either the heap or the stack.

Solution 4 - C

In the accepted answer, the implementation of strdup is presented as:

ptr2 = malloc(strlen(ptr1)+1);
strcpy(ptr2,ptr1);

However, that is somewhat sub-optimal because both strlen and strcpy need to find the length of the string by checking if each character is a \0.

Using memcpy should be more efficient:

char *strdup(const char *src) {
    size_t len = strlen(src) + 1;
    char *s = malloc(len);
    if (s == NULL)
        return NULL;
    return (char *)memcpy(s, src, len);
}

Solution 5 - C

char *strdup(char *pszSrch);

strdup will allocate storage the size of the original string. If storage allocation is successful, the original string is copied to the duplicate string.

strdupd return NULL on failure. If memory is not allocated, copy fails strdup return NULL.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohanView Question on Stackoverflow
Solution 1 - CAbdul MuheedhView Answer on Stackoverflow
Solution 2 - CKerrek SBView Answer on Stackoverflow
Solution 3 - COrenView Answer on Stackoverflow
Solution 4 - CHeikki SalokantoView Answer on Stackoverflow
Solution 5 - CBharath RavindraView Answer on Stackoverflow