strdup(3)
๐ strdup(3) โ string duplicate ๐
okay bestie, so you know how you have a string and you do not wanna mess with the original? yeah. this is that. it's strdup from the Library Functions Manual, and it's literally the moment your string gets a twin. ๐ฏโโ๏ธโจ
๐ LIBRARY
Standard C library (libc, -lc) โ she lives in libc, no cap. ๐ซถ
โจ SYNOPSIS โจ
#include <string.h>
char *strdup(const char *s);
๐ง Feature Test Macro Requirements for glibc ๐ง
(if you wanna know the whole lore on these, go peek at feature_test_macros(7))
strdup():
_XOPEN_SOURCE >= 500
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
๐ธ DESCRIPTION ๐ธ
The strdup() function hands you back a pointer to a brand new string that is a duplicate of the string s. like, a whole copy. its own whole vibe.
The memory for that new string? It's obtained with malloc(3), and you can free it with free(3). so yeah, it's yours, you own it, you have to clean it up. main character responsibilities. ๐
๐ bestie PLEASE โ that memory came frommalloc(3), so it is NOT free real estate. if you don'tfree(3)it, that's a leak. do not ghost your allocations.
๐ RETURN VALUE ๐
On success, strdup() returns a pointer to the duplicated string. iconique. โจ
It returns NULL if there wasn't enough memory available, with errno set to indicate the error. so yeah, check it. fr fr. ๐ญ
๐ฅ ERRORS ๐ฅ
See malloc(3). that's the whole section, she's brief but she's holding the receipts.
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, see attributes(7).
- Interface:
strdup() - Attribute: Thread safety
- Value: MT-Safe ๐
(not me converting the table, the table is cute but the list is cuter ๐ )
โญ STANDARDS โญ
POSIX.1-2008. certified, approved, periodt.
๐ฆ HISTORY ๐ฆ
SVr4, 4.3BSD-Reno, POSIX.1-2001. she's been around. legacy queen behavior. ๐
๐ SEE ALSO ๐
malloc(3), string(3), strdupa(3), wcsdup(3)
the whole friend group, go say hi. ๐ซถ
Linux man-pages 6.20 ยท 2026-08-10 ยท strdup(3) โ mwah, that's the page bestie, go duplicate responsibly and free your memory ๐โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms