1strdup(3) Library Functions Manual strdup(3)
2
3
4
6 strdup, strndup, strdupa, strndupa - duplicate a string
7
9 Standard C library (libc, -lc)
10
12 #include <string.h>
13
14 char *strdup(const char *s);
15
16 char *strndup(const char s[.n], size_t n);
17 char *strdupa(const char *s);
18 char *strndupa(const char s[.n], size_t n);
19
20 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22 strdup():
23 _XOPEN_SOURCE >= 500
24 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
25 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
26
27 strndup():
28 Since glibc 2.10:
29 _POSIX_C_SOURCE >= 200809L
30 Before glibc 2.10:
31 _GNU_SOURCE
32
33 strdupa(), strndupa():
34 _GNU_SOURCE
35
37 The strdup() function returns a pointer to a new string which is a du‐
38 plicate of the string s. Memory for the new string is obtained with
39 malloc(3), and can be freed with free(3).
40
41 The strndup() function is similar, but copies at most n bytes. If s is
42 longer than n, only n bytes are copied, and a terminating null byte
43 ('\0') is added.
44
45 strdupa() and strndupa() are similar, but use alloca(3) to allocate the
46 buffer.
47
49 On success, the strdup() function returns a pointer to the duplicated
50 string. It returns NULL if insufficient memory was available, with er‐
51 rno set to indicate the error.
52
54 ENOMEM Insufficient memory available to allocate duplicate string.
55
57 For an explanation of the terms used in this section, see at‐
58 tributes(7).
59
60 ┌────────────────────────────────────────────┬───────────────┬─────────┐
61 │Interface │ Attribute │ Value │
62 ├────────────────────────────────────────────┼───────────────┼─────────┤
63 │strdup(), strndup(), strdupa(), strndupa() │ Thread safety │ MT-Safe │
64 └────────────────────────────────────────────┴───────────────┴─────────┘
65
67 strdup()
68 strndup()
69 POSIX.1-2008.
70
71 strdupa()
72 strndupa()
73 GNU.
74
76 strdup()
77 SVr4, 4.3BSD-Reno, POSIX.1-2001.
78
79 strndup()
80 POSIX.1-2008.
81
82 strdupa()
83 strndupa()
84 GNU.
85
87 alloca(3), calloc(3), free(3), malloc(3), realloc(3), string(3),
88 wcsdup(3)
89
90
91
92Linux man-pages 6.05 2023-07-20 strdup(3)