What is the rationale for not including strdup in the C Standard?

…衆ロ難τιáo~ 提交于 2019-11-29 06:06:22

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.

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.

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.

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.

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).

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!