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 from malloc(3), so it is NOT free real estate. if you don't free(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms