字符串处理函数族strdup、strndup、strndupa、strdupa
先看一下man手册:man strdup
STRDUP(3) Linux Programmer's Manual STRDUP(3)
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);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strdup():
_SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
strndup():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
Before glibc 2.10:
_GNU_SOURCE
strdupa(), strndupa(): _GNU_SOURCE
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 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 available only when using the GNU GCC suite, and suffer from the same limitations described in
alloca(3).
RETURN VALUE
On success, the strdup() function returns a pointer to the duplicated string. It returns NULL if insufficient memory was available, with errno set to indicate the cause of the error.
ERRORS
ENOMEM Insufficient memory available to allocate duplicate string.
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
┌────────────────────────────────┬───────────────┬─────────┐
│Interface │ Attribute │ Value │
├────────────────────────────────┼───────────────┼─────────┤
│strdup(), strndup(), strdupa(), │ Thread safety │ MT-Safe │
│strndupa() │ │ │
└────────────────────────────────┴───────────────┴─────────┘
CONFORMING TO
strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001. strndup() conforms to POSIX.1-2008. strdupa() and strndupa() are GNU extensions.
SEE ALSO
alloca(3), calloc(3), free(3), malloc(3), realloc(3), string(3), wcsdup(3)
COLOPHON
This page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at
http://www.kernel.org/doc/man-pages/.
GNU 2015-03-29 STRDUP(3)
简介
头文件 #include <string.h>
1,strdup——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).
函数strdup()利用malloc分配字符串s大小的空间,复制s内容到分配的空间,返回指向该空间首地址的指针。说白了,相当于返回s的一份复制体的指针,而且该空间是用malloc分配的,所以在使用完毕后需要free()掉。
2,strdnup——The strndup() function is similar, but copies at most n bytes. If s is longer than n, only n bytes are copied, and a terminating null byte ('\0') is added.
该函数类似上一个函数,不过有两个参数,第二个参数为n,用来指定malloc声明空间的大小。如果s的长度大于n,则只有n个字节被复制,'\0'结束符被加到末尾。
3,strdupa、strndupa—— strdupa() and strndupa() are similar, but use alloca(3) to allocate the buffer. They are available only when using the GNU GCC suite, and suffer from the same limitations described in alloca(3).
这两个函数和前两个函数功能类似,区别在于用alloca分配空间,所以承受着和alloca相同的限制。所以先介绍一下alloca()函数。根据下文【3,alloca()函数的介绍】,所以在使用strdupa、strndupa时候,注意以上alloca的用法和限制。考虑到栈空间的珍贵,建议这两个函数慎重使用!
alloca()函数
ALLOCA(3) Linux Programmer's Manual ALLOCA(3)
NAME
alloca - allocate memory that is automatically freed
SYNOPSIS
#include <alloca.h>
void *alloca(size_t size);
DESCRIPTION
The alloca() function allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller.
RETURN VALUE
The alloca() function returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behavior is undefined.
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
┌──────────┬───────────────┬─────────┐
│Interface │ Attribute │ Value │
├──────────┼───────────────┼─────────┤
│alloca() │ Thread safety │ MT-Safe │
└──────────┴───────────────┴─────────┘
CONFORMING TO
This function is not in POSIX.1.
There is evidence that the alloca() function appeared in 32V, PWB, PWB.2, 3BSD, and 4BSD. There is a man page for it in 4.3BSD. Linux uses the GNU version.
NOTES
The alloca() function is machine- and compiler-dependent. For certain applications, its use can improve efficiency compared to the use of malloc(3) plus free(3). In certain cases, it can also
simplify memory deallocation in applications that use longjmp(3) or siglongjmp(3). Otherwise, its use is discouraged.
Because the space allocated by alloca() is allocated within the stack frame, that space is automatically freed if the function return is jumped over by a call to longjmp(3) or siglongjmp(3).
Do not attempt to free(3) space allocated by alloca()!
Notes on the GNU version
Normally, gcc(1) translates calls to alloca() with inlined code. This is not done when either the -ansi, -std=c89, -std=c99, or the -std=c11 option is given and the header <alloca.h> is not
included. Otherwise, (without an -ansi or -std=c* option) the glibc version of <stdlib.h> includes <alloca.h> and that contains the lines:
#ifdef __GNUC__
#define alloca(size) __builtin_alloca (size)
#endif
with messy consequences if one has a private version of this function.
The fact that the code is inlined means that it is impossible to take the address of this function, or to change its behavior by linking with a different library.
The inlined code often consists of a single instruction adjusting the stack pointer, and does not check for stack overflow. Thus, there is no NULL error return.
BUGS
There is no error indication if the stack frame cannot be extended. (However, after a failed allocation, the program is likely to receive a SIGSEGV signal if it attempts to access the unallo‐
cated space.)
On many systems alloca() cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca() would appear on the stack in the middle of the space for the
function arguments.
SEE ALSO
brk(2), longjmp(3), malloc(3)
COLOPHON
This page is part of release 4.04 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at
http://www.kernel.org/doc/man-pages/.
GNU 2015-03-02 ALLOCA(3)
1,对比介绍:
alloca()函数和malloc函数功能类似,用于分配空间。区别在于,alloca分配的是调用者的栈空间,当函数结束返回时这段空间会自动释放。而malloc分配的堆空间,不会自动释放,需要调用free()函数手动释放。
2,优缺点:
alloca函数优点:效率比malloc和free的要高;简化了空间处理回收;
缺点:由于栈空间较小,分配的大小受限;空间不够时,可能不会报错;不能用于作为函数调用的形参;
3,注意事项:不可调用free函数。
总结
strdup族函数简单实用,简化了复制字符串的步骤。但是,使用strdup、strndup函数记得free函数的调用;而strdupa、strndupa注意alloca的限制。