strlen 不检查 NULL [英] strlen not checking for NULL

查看:13
本文介绍了strlen 不检查 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 strlen() 不检查 NULL?

Why is strlen() not checking for NULL?

如果我这样做 strlen(NULL),程序分段错误.

if I do strlen(NULL), the program segmentation 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆