1. 程式人生 > >Linux系統呼叫函式strdup

Linux系統呼叫函式strdup

Name

strdup, strndup, strdupa, strndupa - duplicate a string 複製一個字串

Synopsis

#include <string.h>
char *strdup(const char *s);
char *strndup(const char *s, size_t n);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t n);

Description

The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

The strndup() function is similar, but only copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte (’\0’) is added.

strdupa() and strndupa() are similar, but use alloca(3) to allocate the buffer. They are only available when using the GNU GCC suite, and suffer from the same limitations described in alloca(3).

strdup()函式返回一個指向新字串的指標,該字串是字串s的副本。 使用malloc(3)獲取新字串的記憶體,可以使用free(3)釋放。

strndup()函式類似,但只複製最多n個位元組。 如果s大於n,則僅複製n個位元組,並新增終止空位元組(’\ 0’)。

strdupa()和strndupa()類似,但使用alloca(3)來分配緩衝區。 它們僅在使用GNU GCC套件時可用,並且受到alloca(3)中描述的相同限制。

Return Value

The strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available. strdup()函式返回指向重複字串的指標,如果記憶體不足,則返回NULL。

Errors

ENOMEM

Insufficient memory available to allocate duplicate string. 記憶體不足,無法分配重複的字串。