Linux下C语言之字符串处理函数strdup、strndup、strndupa、strdupa的使用介绍

本文主要对Linux下的C语言字符串处理函数strdup族函数进行介绍,分别是strdup、strndup、strndupa、strdupa。下面进入正题。


一、函数族strdup、strndup、strndupa、strdupa

1,先看一下man手册:man strdup

      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 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 suf‐
       fer 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.


2,介绍

头文件  #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的用法和限制。考虑到栈空间的珍贵,建议这两个函数慎重使用!

3,alloca()函数

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 allo‐
       cated space.  If the allocation causes stack overflow, program behavior
       is undefined.
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).
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 unallocated 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  argu‐
       ments.

1,对比介绍:
    alloca()函数和malloc函数功能类似,用于分配空间。区别在于,alloca分配的是调用者的栈空间,当函数结束返回时这段空间会自动释放。而malloc分配的堆空间,不会自动释放,需要调用free()函数手动释放。

2,优缺点:
    alloca函数优点:效率比malloc和free的要高;简化了空间处理回收;

    缺点:由于栈空间较小,分配的大小受限;空间不够时,可能不会报错;不能用于作为函数调用的形参;

    
3,注意事项:不可调用free函数。

二、总结


      strdup族函数简单实用,简化了复制字符串的步骤。但是,使用strdup、strndup函数记得free函数的调用;而strdupa、strndupa注意alloca的限制。

猜你喜欢

转载自blog.csdn.net/Brouce__Lee/article/details/81220663