C语言 strlen 不检查 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5796103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:30:02  来源:igfitidea点击:

strlen not checking for NULL

csegmentation-faultstrlen

提问by hari

Why is strlen()not checking for NULL?

为什么strlen()不检查 NULL?

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

如果我这样做strlen(NULL),程序段就会出错。

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

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

回答by Hogan

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

c 中的标准模板如下所示

 int someStrLen;

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

回答by John Bode

The portion of the language standardthat defines the string handling library states that, unless specified otherwise for the specific function, any pointer arguments musthave 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 unnecessaryruntime 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 knowyou 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 unnecessarycheck against NULL.

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

回答by Stoian Ivanov

A little macro to help your grief:

一个帮助你悲伤的小宏:

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

回答by ninjalj

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

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

回答by Casey Flynn

size_t strlen ( const char * str );

http://www.cplusplus.com/reference/clibrary/cstring/strlen/

http://www.cplusplus.com/reference/clibrary/cstring/strlen/

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

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

回答by Blagovest Buyukliev

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 strlenthousands of times and always doing str != NULLis 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.

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

  • 它会导致运行时开销——调用strlen数千次并且总是这样做str != NULL是不合理的,除非程序员被视为娘娘腔;

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