strdup() Copy string
来源:互联网 发布:it服务项目经理证书 编辑:程序博客网 时间:2024/05/14 17:20
It's effectively doing the same as the following code:
char *strdup (const char *s) { char *d = malloc (strlen (s) + 1); // Space for length plus nul if (d == NULL) return NULL; // No memory strcpy (d,s); // Copy the characters return d; // Return the new string}
In other words:
- It tries to allocate enough memory to hold the old string (plus a null character to mark the end of the string).
- If the allocation failed, it sets
errno
toENOMEM
and returnsNULL
immediately (setting oferrno
toENOMEM
is somethingmalloc
does so we don't need to explicitly do it in ourstrdup
). - Otherwise the allocation worked so we copy the old string to the new string and return the new address (which the caller is responsible for freeing).
Keep in mind that's the conceptual definition. Any library writer worth their salary should have provided heavily optimised code targeting the particular processor being used.
- strdup() Copy string
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup
- strdup()
- strdup
- strdup
- uva 11292
- Makefile :Makefile 的几个自动变量说明 $@ $< $^
- 一些异常收集
- SQL Server 2012 SP1 补丁发布
- 无线网卡和无线上网卡、无线路由器和无线AP、WIFI和WinMax
- strdup() Copy string
- listcontrol的第一项
- 遍历时遇到出错跳出循环的解决办法
- oracle 中TRANSLATE详解
- [Happy Coding] C++中的多态遇上数组
- 你不能忽视的HTML语言
- oracle连接
- 启动服务器,注意端口被占用
- radgrid column filter, filtered by a “From” and “To” date, inclusive