Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

strndupa(3C)

Name

strdup, strndup, strdupa, strndupa - string duplication

Synopsis

#include <string.h>

char *strdup(const char *s);
char *strndup(const char *s, size_t size);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t size);

Description

These functions return a pointer to a newly allocated string (array of characters) which is a duplicate of the string s. Depending on the function, the provided strings may be either terminated by a null character or bounded by a maximum length of n bytes. The newly allocated strings will always be null-terminated.

All character counts are measured in individual bytes, even if a string with multibyte characters is used, which may result in copying only part of a multibyte character if the full character does not fit within the byte count specified. They do not check for null pointers, and programs may crash if passing null or otherwise invalid pointers to these functions.

The strdup() function returns a pointer to a new string that is a duplicate of the null-terminated string pointed to by s. The returned pointer can be passed to free(). The space for the new string is obtained using malloc(3C). If the new string cannot be created, a null pointer is returned and errno may be set to ENOMEM to indicate that the storage space available is insufficient.

The strndup() function is similar to strdup(), except that it copies at most size bytes. If the length of s is larger than size, only size bytes are copied and a terminating null byte is added. If size is larger than the length of s, all bytes in s are copied, including the terminating null character.

The strdupa() and strndupa() functions are similar to strdup() and strndup(), respectively, but use alloca(3C) to allocate the buffer.

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
See below
Standard
See below

MT-Level

The strdup() and strndup() functions are MT-Safe.

The strdupa() and strndupa() functions are Async-Signal-Safe.

Standard

See standards(7) for descriptions of the following standards:

INTERFACES
APPLICABLE STANDARDS
strdup()
  • POSIX.1-2001 through 2008,
  • SUSv3 through SUSv4,
  • XPG6 through XPG7
strndup()
  • POSIX.1-2008,
  • SUSv4,
  • XPG7
strdupa(), strndupa()
None

See Also

alloca(3C), malloc(3C), string(3C), wcsdup(3C), attributes(7), standards(7)

History

The strndup(), strdupa(), and strndupa() functions were added to Oracle Solaris in the Solaris 11.0.0 release.

The strdup() function has been included in all Sun and Oracle releases of Solaris.