什么是不包括在C标准的strdup什么道理呢? [英] What is the rationale for not including strdup in the C Standard?
问题描述
大多数C程序员所熟悉的的strdup
功能。他们中许多人会想当然的,但它不是C标准(C89没有,C99也不C11)的一部分。它是POSIX的一部分,并且可能并不适用于所有的环境。事实上,微软坚持将其改名 _strdup
,会造成混乱。
Most C programmers are familiar with the strdup
function. Many of them will take it for granted, yet it is not part of the C Standard (neither C89, C99 nor C11). It is part of POSIX and may not be available on all environments. Indeed Microsoft insisted on renaming it _strdup
, adding to confusion.
这是相当容易界定这种方式(在C):
It is rather easy to define it this way (in C):
#include <string.h>
char *strdup(const char *s) {
size_t size = strlen(s) + 1;
char *p = malloc(size);
if (p) {
memcpy(p, s, size);
}
return p;
}
但即使是精明的程序员可以很容易地得到它错了。
But even savvy programmers can easily get it wrong.
此外,仅在没有它的系统重新定义函数证明有点复杂如下解释:的strdup()函数
Furthermore, redefining the function only on systems that do not have it proves a bit complicated as explained here: strdup() function
为什么不包含在C标准的修订版实用等广泛支持的功能呢?很多新的功能已被列入C99 C标准库,什么是不包括理的strdup
?
Why not include such useful widely supported functions in revised editions of the C Standard? A lot of new functions have been included the C standard library in C99, what is the rationale for not including strdup
?
推荐答案
在评论中引用链接(<一个href=\"http://open-std.org/JTC1/SC22/WG14/www/docs/n718.htm\">http://open-std.org/JTC1/SC22/WG14/www/docs/n718.htm)提供有关什么是错误的关于标准库的strdup有一个解释:
The quoted link in the comments (http://open-std.org/JTC1/SC22/WG14/www/docs/n718.htm) gives an explanation about what is "wrong" about having strdup in the standard library:
的主要问题是增加一个功能,到为用户自动分配堆内存的标准库是可取的。
The major issue was the desirability of adding a function to the standard library which allocates heap memory automatically for the user.
基本上,C语言和标准库尽量不要把关于用户分配和如何使用内存的假设。结果
它提供了一些设施,其中有堆,堆。
Basically, the C language and its standard library try their best not to make assumptions about how the user allocates and uses memory.
It gives a few facilities among which are the stack, and the heap.
虽然的malloc /自由是标准化的动态内存分配,他们绝不是唯一这样做的方式,因为动态内存管理是一个非常复杂的话题,各类应用程序的默认分配策略可能不是可取的。
While malloc/free are standardized for dynamic memory allocation, they are by no means the only way to do so, because dynamic memory management is a very complicated topic and the default allocation strategy might not be desirable for all kinds of applications.
有例如一些独立的库如 jemalloc 强调低碎片和并发性的,甚至是完全的垃圾收藏家如的贝姆,德默斯 - 韦泽保守的垃圾收集器。
这些图书馆提供的是为了在更换专门用于标准* alloc和free函数从&LT的malloc /自由的实现;文件stdlib.h&GT;
不破,其余的兼容性C标准库。
There are for example a few independant libraries such as jemalloc which emphasizes low fragmentation and concurrency, or even full-fledged garbage collectors such as The Boehm-Demers-Weiser conservative garbage collector.
These libraries offer malloc/free implementations that are meant to be used exclusively in replacement to the standard *alloc and free functions from <stdlib.h>
without breaking compatibility with the rest of the C standard library.
所以,如果的strdup制成的标准,这将有效地使用第三方内存管理功能正在使用code取消比赛资格(但必须指出的是,上述jemalloc库确实提供的strdup的实现,以避免这个问题)。
So if strdup was made standard, it would effectively be disqualified from being used by code using third-party memory management functions (it must be noted that the aforementioned jemalloc library does provide an implementation of strdup to avoid this problem).
更一般地说,而的strdup无疑是一个实用的功能,它从一个缺乏明确其语义的困扰。它是在&LT声明的函数; string.h中&GT;
头,但调用它需要通过调用从而释放返回的缓冲区中的免费
从&LT功能;文件stdlib.h&GT;
头。因此,它是一个字符串函数或具有记忆功能?结果
POSIX标准中离开它似乎是最合理的解决方案,以避免使C标准库不太清楚。
More generally speaking, while strdup certainly is a practical function, it suffers from a lack of clarity in its semantics. It is a function declared in the <string.h>
header, but calling it requires to consequently free the returned buffer by calling the free
function from the <stdlib.h>
header. So, is it a string function or a memory function ?
Leaving it in the POSIX standard seems to be the most reasonable solution to avoid making the C standard library less clear.
这篇关于什么是不包括在C标准的strdup什么道理呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!