strlen的不检查NULL [英] strlen not checking for NULL
问题描述
为什么的strlen()
不检查NULL?
Why is strlen()
not checking for NULL?
如果我这样做的strlen(NULL)
,它赛格故障。
if I do strlen(NULL)
, it seg faults.
试图了解它背后的基本原理(如果有的话)。
Trying to understand the rationale behind it (if any).
推荐答案
理性的背后却是简单的 - 你怎么能检查的东西,不存在长度
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).
在C标准模板看起来像这样
A standard template in c would look like this
int someStrLen;
if (someStr != NULL) // or if (someStr)
someStrLen = strlen(someStr);
else
{
// handle error.
}
这篇关于strlen的不检查NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!