strlen 不检查 NULL

发布于 2024-11-03 15:10:44 字数 120 浏览 0 评论 0原文

为什么 strlen() 不检查 NULL?

如果我执行strlen(NULL),程序就会出现分段错误。

尝试了解其背后的基本原理(如果有)。

Why is strlen() not checking for NULL?

if I do strlen(NULL), the program segmentation faults.

Trying to understand the rationale behind it (if any).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

阳光下慵懒的猫 2024-11-10 15:10:44

其背后的原理很简单——如何检查不存在的东西的长度?

此外,与“托管语言”不同,运行时系统不期望正确处理无效数据或数据结构。 (此类问题正是为什么更“现代”的语言对于非计算或性能要求较低的应用程序更受欢迎的原因)。

C 中的标准模板如下所示

 int someStrLen;

 if (someStr != NULL)  // or if (someStr)
    someStrLen = strlen(someStr);
 else
 {
    // handle error.
 }

The rational behind it is simple -- how can you check the length of something that does not exist?

Also, unlike "managed languages" there is no expectations the run time system will handle invalid data or data structures correctly. (This type of issue is exactly why more "modern" languages are more popular for non-computation or less performant requiring applications).

A standard template in c would look like this

 int someStrLen;

 if (someStr != NULL)  // or if (someStr)
    someStrLen = strlen(someStr);
 else
 {
    // handle error.
 }
○闲身 2024-11-10 15:10:44

语言标准的部分,定义了字符串处理库规定,除非为特定函数另外指定,否则任何指针参数必须具有有效值。

C 标准库设计背后的理念是,程序员最终最有能力知道是否确实需要执行运行时检查。回到以千字节为单位测量总系统内存的时代,执行不必要的运行时检查的开销可能会非常痛苦。所以 C 标准库不会费心做任何这些检查;如果确实有必要的话,它假设程序员已经完成了。如果您知道您永远不会将错误的指针值传递给strlen(例如,您传递的是字符串文字或本地分配的数组),那么就没有需要对 NULL 进行不必要的检查,从而使生成的二进制文件变得混乱。

The portion of the language standard that defines the string handling library states that, unless specified otherwise for the specific function, any pointer arguments must have valid values.

The philosphy behind the design of the C standard library is that the programmer is ultimately in the best position to know whether a run-time check really needs to be performed. Back in the days when your total system memory was measured in kilobytes, the overhead of performing an unnecessary runtime check could be pretty painful. So the C standard library doesn't bother doing any of those checks; it assumes that the programmer has already done it if it's really necessary. If you know you will never pass a bad pointer value to strlen (such as, you're passing in a string literal, or a locally allocated array), then there's no need to clutter up the resulting binary with an unnecessary check against NULL.

花想c 2024-11-10 15:10:44

该标准并不要求它,因此实现只是避免了测试和潜在的昂贵的跳跃。

The standard does not require it, so implementations just avoid a test and potentially an expensive jump.

知足的幸福 2024-11-10 15:10:44

一个小宏可以缓解你的悲伤:

#define strlens(s) (s==NULL?0:strlen(s))

A little macro to help your grief:

#define strlens(s) (s==NULL?0:strlen(s))
暗喜 2024-11-10 15:10:44

三个重要原因:

  • 标准库和 C 语言的设计假设程序员知道他在做什么,因此空指针不会被视为边缘情况,而是被视为程序员的错误,导致未定义的行为 标准库和 C 语言的设计假设

  • 它会产生运行时开销 - 调用 strlen 数千次并且总是执行 str != NULL 是不合理的,除非程序员被视为娘娘腔;

  • 它增加了代码大小 - 它可能只有几条指令,但如果您采用这一原则并在任何地方都这样做,它会显着增加您的代码。

Three significant reasons:

  • The standard library and the C language are designed assuming that the programmer knows what he is doing, so a null pointer isn't treated as an edge case, but rather as a programmer's mistake that results in undefined behaviour;

  • It incurs runtime overhead - calling strlen thousands of times and always doing str != NULL is not reasonable unless the programmer is treated as a sissy;

  • It adds up to the code size - it could only be a few instructions, but if you adopt this principle and do it everywhere it can inflate your code significantly.

好多鱼好多余 2024-11-10 15:10:44
size_t strlen ( const char * str );

https://www.cplusplus.com/reference/cstring/strlen

Strlen 采用指向字符数组的指针作为参数,null 不是此函数的有效参数。

size_t strlen ( const char * str );

https://www.cplusplus.com/reference/cstring/strlen

Strlen takes a pointer to a character array as a parameter, null is not a valid argument to this function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文