一、函数族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()函数

NAMEalloca - allocate memory that is automatically freedSYNOPSIS#include <alloca.h>void *alloca(size_t size);DESCRIPTIONThe  alloca() function allocates size bytes of space in the stack frameof the caller.  This temporary space is automatically  freed  when  thefunction that called alloca() returns to its caller.RETURN VALUEThe  alloca()  function returns a pointer to the beginning of the allo‐cated space.  If the allocation causes stack overflow, program behavioris undefined.
NOTESThe alloca() function is machine- and compiler-dependent.  For  certainapplications,  its  use  can  improve efficiency compared to the use ofmalloc(3) plus free(3).  In certain cases, it can also simplify  memorydeallocation  in  applications  that  use  longjmp(3) or siglongjmp(3).Otherwise, its use is discouraged.Because the space allocated by alloca() is allocated within  the  stackframe,  that  space  is  automatically  freed if the function return isjumped over by a call to longjmp(3) or siglongjmp(3).
BUGSThere is no error indication if the stack  frame  cannot  be  extended.(However, after a failed allocation, the program is likely to receive aSIGSEGV signal if it attempts to access the unallocated space.)On many systems alloca() cannot be used inside the list of arguments ofa  function  call,  because  the stack space reserved by alloca() wouldappear 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的限制。

Linux字符串处理函数strdup、strndup、strndupa、strdupa相关推荐

  1. 字符串拷贝函数strdup()

    char *strdup(const char *s); 注: strdup()函数是c语言中常用的一种字符串拷贝库函数,一般和free()函数成对出现. 字符串拷贝函数strdup()内部实现如下: ...

  2. 字符串复制函数strdup和_strdup

    字符串复制函数strdup和_strdup 函数原型 #include <string.h> char *strdup(const char *s); strdup()函数是c语言中常用的 ...

  3. linux 字符串转换函数 simple_strtoul 简介

    Linux内核中提供的一些字符串转换函数: lib/vsprintf.c unsigned long long simple_strtoull(const char *cp, char **endp, ...

  4. Linux字符串转换函数汇总

    字符串转换篇 atof atoi atol gcvt strtod strtol strtoul toascii tolower toupper (1)atof(将字符串转换成浮点型数)  相关函数  ...

  5. linux c中字符替换函数,Linux C 支持正则表达式的字符串替换函数

    [root@localhost src]# cat a.c /** * Linux C 支持正则表达式的字符串替换函数 * * Author: cnscn@163.com * Homepage: ww ...

  6. linux 内核 字符串转换函数

    Linux内核中提供的一些字符串转换函数: lib/vsprintf.c unsigned long long simple_strtoull(const char *cp, char **endp, ...

  7. linux c数字转字符串函数,Linux常用C函数—字符串转换篇

    Linux 常用C 函数-字符串转换篇 atof (将字符串转换成浮点型数) 相关函数 atoi ,atol ,strtod ,strtol ,strtoul 定义函数 double atof(con ...

  8. linux 算法函数,数据结构——算法之(012)( linux C 全部字符串操作函数实现)...

    数据结构--算法之(012)( linux C 所有字符串操作函数实现) 题目:实现linux C下常用的字符串操作函数 题目分析: 一.面试中可能经常遇到这样的问题:比如strcpy.memcpy. ...

  9. linux下通过字符串调用函数,linux中字符串转换函数 simple_strtoul

    转自 http://blog.csdn.net/tommy_wxie/article/details/7480087 Linux内核中提供的一些字符串转换函数: lib/vsprintf.c 1. u ...

最新文章

  1. RDKit | BCUT:基于分子图结构的二维描述符
  2. 坐标系转换公式_【技术】西安80坐标与地方坐标系的转换方法技巧
  3. python construct_Python construct包_程序模块 - PyPI - Python中文网
  4. python不能处理excel文件-python处理Excel文件
  5. Python学习笔记《Python核心编程》第4章Python对象
  6. thttpd+php 不加载php.ini 问题
  7. python 利用 for ... else 跳出双层嵌套循环
  8. MySQL性能的五大配置参数(内存参数)
  9. 《程序员代码面试指南》第二章 链表问题 搜索二叉树转换为双向链表
  10. python自动答题软件_广东开放大学(广开)线上作业自动答题python-selenium
  11. python公式如何编写_如何编写 Python 程序,资深Python大咖教你玩转Python
  12. STL(三)——next_permutation()
  13. 保证MQ消费消息的幂等性,真可以用版本号的方式?
  14. (八)Hibernate的一对多关联关系
  15. Python中[::-1]实现翻转列表的原理
  16. C# winform窗体实现图片轮播
  17. 深入理解Nginx及使用Nginx实现负载均衡
  18. WPF 自定义 写实风 雷达图控件
  19. HTML - 03 网页元素的属性
  20. rgb html转换,RGB与十六进制数值互转(html)

热门文章

  1. NRF2401资料和无线调参上位机分享
  2. 万达商管再冲刺上市:承诺三年要赚220亿元,王健林夫妇提前套现
  3. 三天学会Mysql之第(三)天:创建修改删除
  4. 服务器自动获取169.254,4种可能导致出现169.254 IP地址段而上不了网的解决方法
  5. 力扣 字符串 常用函数总结 cnt [ ch-‘a‘ ]++ 位运算 string转int ,char 转int
  6. 已知 8253的端口地址为0200H~0203H,将8253CNT0 设置为方式1,计数初值为3000H, CNT1设置为方式2,计数初值为2010H, CNT2设置为方式4,计数初值为4030H。试
  7. QQ超市最佳路径寻路算法改进尝试3
  8. Oracle账号_Oracle数据库账号总是被锁?通过这里就可以发现是哪个IP造成的
  9. 0107连通分量-无向图-数据结构和算法(Java)
  10. vue 获取屏幕宽度尺寸