- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在 C 中,memmem
函数用于定位内存区域中的特定字节序列。它可以同化为 strstr
,专门用于以空字符结尾的字符串。
这个函数作为 GNU 扩展提供,而不是直接在标准库中,有什么特别的原因吗?手册指出:
This function was broken in Linux libraries up to and including libc 5.0.9; there the needle and haystack arguments were interchanged, and a pointer to the end of the first occurrence of needle was returned.
Both old and new libc's have the bug that if needle is empty, haystack-1 (instead of haystack) is returned. And glibc 2.0 makes it worse, returning a pointer to the last byte of haystack. This is fixed in glibc 2.1.
我可以看到它经过了多次修复,但我想知道为什么它在某些发行版上没有像 strstr
那样直接可用(如果不是更多的话)。它是否仍然会带来实现问题?
编辑(动机):如果标准以相反的方式决定,我不会问这个问题:包括 memmem
但不包括 strstr
。实际上,strstr
可能类似于:
memmem(str, strlen(str), "search", 6);
有点棘手,但考虑到在 C 函数中通常需要 data chunk 及其 length。
编辑 (2):评论和回答的另一个动机。报价 Theolodis :
Not every function is necessary to every single, or at least most of the C developers, so it would actually make the standard libraries unnecessarily huge.
嗯,我完全同意,我一直致力于使图书馆更轻、更快。但是...为什么 strncpy
和 memcpy
(来自 keltar 的评论)...?我几乎可以问:为什么可怜的 memmem
被“黑羊”了?
最佳答案
从历史上看,在标准的第一个修订版之前,C 已经由编译器编写者编写。
strstr
的情况有点不同,因为它是由 C 委员会引入的,C89 Rationale 文档告诉我们:
"The
strstr
function is an invention of the Committee. It is included as a hook for efficient algorithms, or for built-in substring instruction."
C 委员会没有解释为什么它没有制定更通用的功能,不仅限于字符串,因此任何推理都可能只是推测。我唯一的猜测是用例被认为不够重要,不能使用通用的 memmem
而不是 strstr
。请记住,在 C 的目标中,有这个要求(在 C99 基本原理中)“保持语言小而简单”。甚至 POSIX 也没有考虑将其包含在内。
在任何情况下,据我所知,没有人提出任何缺陷报告或包含 memmem
的建议。
关于c - memmem 是 GNU 扩展有什么特别的原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263582/
我已经可以在其中输入一些附加文本了mydomain/wiki/Special:UserLogin。我添加了一句话: In order to apply for an account send an m
有人可以解释以下脚本输出背后的逻辑吗? import numpy if(numpy.dtype(numpy.float64) == None): print "Surprise!!!!" 谢谢
是我还是 gmail bulls**t?在 outlook/浏览器上,我的电子邮件是完美的,但在 gmail 上,2 个表之间有一个空间,为什么?!?图片:http://i.imgur.com/srJ
我是一名优秀的程序员,十分优秀!