Linux字符串处理函数strdup、strndup、strndupa、strdupa

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

strdup函数原型:

strdup()主要是拷贝字符串s的一个副本,由函数返回值返回,这个副本有自己的内存空间,和s不相干。strdup函数复制一个字符串,使用完后要记得删除在函数中动态申请的内存,strdup函数的参数不能为NULL,一旦为NULL,就会报段错误,因为该函数包括了strlen函数,而该函数参数不能是NULL。

strdup的工作原理:

char * __strdup (const char *s)

{

size_t len =strlen (s) + 1;

void *new =malloc (len);

if (new == NULL)

return NULL;

return (char *)memcpy (new, s, len);

}

实例1:

C/C++ code

#include <stdio.h>

#include <string.h>

#include <alloc.h>

int main(void)

{

        char *dup_str,*string = "abcde";

        dup_str =strdup(string);

        printf("%s\n", dup_str);

         free(dup_str);

         return 0;

}

实例2:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

unsigned int Test()

{

    char buf[]="Hello,World!";

    char* pb =strndup(buf, strlen(buf));

    return (unsigned int)(pb);

}

int main()

{

unsigned int pch= Test();

printf("Testing:%s\n", (char*)pch);

free((void*)pch);

return 0;

}

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/wteruiycbqqvwt/article/details/114363507
今日推荐