STRDUP(3POSIX)
π STRDUP(3POSIX) β the POSIX Programmer's Manual, bestie edition π¦
okay so quick vibe check before we even start: this page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so pls consult the corresponding Linux manual page for the actual Linux behavior), or honestly the interface might not even be implemented on Linux at all. π just so we're clear.
β¨ NAME β¨
strdup, strndup β duplicate a specific number of bytes from a string π
πΈ SYNOPSIS πΈ
#include <string.h>
char *strdup(const char *s);
char *strndup(const char *s, size_t size);
π DESCRIPTION π
so strdup() shall return a pointer to a new string, and that new string is a duplicate of the string pointed to by s. the returned pointer can be passed to free(). and if the new string cannot be created? a null pointer is returned. no drama, just null.
strndup() shall be equivalent to the strdup() function β like, it duplicates the provided s in a new block of memory allocated as if by using malloc() β with one exception: strndup() copies at most size plus one bytes into the newly allocated memory, terminating the new string with a NUL character. π
the deets:
- if the length of
sis larger thansize, onlysizebytes shall be duplicated. - if
sizeis larger than the length ofs, all bytes insshall be copied into the new memory buffer, including the terminatingNULcharacter. - the newly created string shall always be properly terminated. periodt. β¨
π₯ RETURN VALUE π₯
on success, strdup() shall return a pointer to a new string. otherwise, it shall return a null pointer and set errno to indicate the error. π
upon successful completion, strndup() shall return a pointer to the newly allocated memory containing the duplicated string. otherwise, it shall return a null pointer and set errno to indicate the error. same energy, fr fr.
π ERRORS π
these functions shall fail if:
ENOMEMβ storage space available is insufficient. that's it, that's the whole list, the memory said no. π«
the following sections are informative. (aka: cute but not binding, iykyk)
π EXAMPLES π
none. literally none. iconic of them honestly. π
π§ APPLICATION USAGE π§
for functions that allocate memory as if by malloc(), the application should release such memory when it is no longer required, by a call to free(). for strdup() and strndup(), this is the return value. so like, free your stuff bestie, don't leak. π
also important: implementations are free to malloc() a buffer containing either (size + 1) bytes or (strnlen( s, size) + 1) bytes. applications should not assume that strndup() will allocate (size + 1) bytes when strlen( s) is smaller than size. no cap, don't assume it.
β RATIONALE β
none. π€·
π FUTURE DIRECTIONS π
none. π€· (again)
π SEE ALSO π
free()wcsdup()
and from the Base Definitions volume of POSIX.1β2017: <string.h> π
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. π«Ά
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! brought to you by the fine people at IEEE/The Open Group, dated 2017, page tag STRDUP(3POSIX). mwah. πβ¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms