strdup(3) Library Functions Manual strdup(3)


strdupstring duplication


#define _XOPEN_SOURCE || _POSIX_C_SOURCE >= 200809L
#include <string.h>

char *strdup(const char *str);

#define _POSIX_C_SOURCE >= 200809L
#include <string.h>

char *strndup(const char *str, size_t len);


strdup() allocates a new buffer of sufficient size as to be able to hold the entirety of str, including the terminating character, and copies the contents of str into it.

strndup() allocates a buffer large enough to contain len characters, plus a trailing null character, or sufficient to contain the entirety of str including the trailing null character, whichever is smaller. The first len characters of str are then copied into it, and a null character appended.

The buffers returned by these functions must be released by a call to free().


free(3) malloc(3) strcpy(3) strncpy(3)


strdup() first specified in X/Open Portability Guide Issue 3 (“XPG3”). Moved into POSIX, and strndup() added, with IEEE Std 1003.1-2008 (“POSIX.1”).
February 20, 2013 Fusion