#include <iostream>
using namespace std;int main()
{int a;char *name = NULL;a = strlen(name);return 0;
}

以上程序编译没问题,运行将会报错。

原因就是name为NULL,strlen参数不能为NULL,为探究原因,我查看了glibc对strlen函数的实现代码如下:

#include <string.h>
#include <stdlib.h>#undef strlen#ifndef STRLEN
#define STRLEN strlen
#endifsize_t STRLEN(const char *str)
{const char *char_ptr;const unsigned long int *longword_ptr;unsigned long int longword, himagic, lomagic;//Handle the first few characters by reading one character at a time.//Do this until CHAR_PTR is aligned on a longword boundary.for (char_ptr = str; ((unsigned long int) char_ptr & (sizeof(longword) - 1)) != 0; ++char_ptr)if (*char_ptr == '\0') return char_ptr - str;longword_ptr = (unsigned long int *) char_ptr;/*Bits 31, 24, 16, and 8 of this number are zero. Call these bits the"holes." Note that there is a hole just to the left of each byte, with an extra at the end:bits: 01111110 11111110 11111110 11111111bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDDThe 1-bits make sure that carries propagate to the next 0-bit.The 0-bits provide holes for carries to fall into.*/himagic = 0x80808080L;lomagic = 0x01010101L;if (sizeof(longword) > 4) {/* 64-bit version of the magic. *//* Do the shift in two steps to avoid a warning if long has 32 bits. */himagic = ((himagic << 16) << 16) | himagic;lomagic = ((lomagic << 16) << 16) | lomagic;}if (sizeof(longword) > 8) abort();/* Instead of the traditional loop which tests each characters,we will test a longword at a time. The tricky part is testingif *any of the four* bytes in the longword in question are zero. */for (;;) {longword = *longword_ptr++; //strlen函数为什么不能传入空指针原因!if (((longword - lomagic) & ~longword & himagic) != 0) {/*Which of the bytes was the zero? If none of them were, it wasa misfire; continue the search. */const char *cp = (const char *)(longword_ptr - 1);if (cp[0] == 0)return cp - str;if (cp[1] == 0)return cp - str + 1;if (cp[2] == 0)return cp - str + 2;if (cp[3] == 0) return cp - str + 3;if (sizeof(longword) > 4) {if (cp[4] == 0) return cp - str + 4;if (cp[5] == 0)return cp - str + 5;if (cp[6] == 0)return cp - str + 6;if (cp[7] == 0)return cp - str + 7;}}}
}

从第47行可见,如果传入NULL,则

longword = *longword_ptr++;

longword_ptr为NULL,从而*longword_ptr是不可访问的。

注:*longword_ptr++等效于*(longword_ptr++), 即先求*longword_ptr,再对longword_ptr+1。

转自:https://blog.csdn.net/a3192048/article/details/80933476

C/C++ strlen函数为什么不能传入空指针NULL?相关推荐

  1. C语言strlen函数与sizeof函数的区别

    strlen函数与sizeof函数的区别 strlen函数和sizeof函数都可以用于获取字符串的长度,但是它们有几个重要的区别. strlen函数返回字符串的长度,而sizeof函数返回的是整个数组 ...

  2. 【C语言】strlen函数的讲解和模拟实现

    文章目录 strlen函数的讲解 strlen函数的模拟实现 计数器方式 递归的方式 指针减指针的方式 strlen函数的讲解 strlen函数我们应该不陌生,它可以帮助我们求字符串的长度(不包括'\ ...

  3. 3种方法实现strlen函数

    在我们使用C语言写代码时,我们常常会用到strlen函数,你是否好奇过strlen是如何实现的呢?接下来,我将会使用3中方法实现strlen函数. 文章目录 1.计数器法 2.递归法 3.指针减指针法 ...

  4. 嵌入式常见的段错误死机原因之一strlen函数

    在做嵌入式产品常见死机原因有很多种,其中之一就是strlen函数引起的.这个函数大家不会陌生,学c语言最基本的函数了,但在实际使用中偶尔不小心还是可能会引起段错误造成死机的.为什么呢?    函数原型 ...

  5. C语言strlen函数详细讲解

    1.strlen函数的功能和字符串的基础知识 计算字符串的长度,返回的是字符串长度,准确来说,返回的是字符串中'\0'之前的字符个数(不包含'\0'),并且是无符号类型. 在C语言中,没有字符串这种变 ...

  6. c语言strlen函数的作用是什么,c语言strlen函数的使用方法是什么

    c语言strlen函数的使用方法是什么 发布时间:2020-08-20 11:54:33 来源:亿速云 阅读:116 作者:小新 小编给大家分享一下c语言strlen函数的使用方法是什么,希望大家阅读 ...

  7. C语言strlen函数的模拟实现

    目录 使用计数方法实现strlen函数的模拟实现 使用递归的方式去实现 使用指针的方式去模拟实现strlen 使用计数方法实现strlen函数的模拟实现 在这里有一个assert函数这是断言,防止空指 ...

  8. PHP strlen()函数和strpos()函数

    strlen()  函数返回字符串的长度(字符数) 代码: <?php echo strlen("Hello world!"); ?> 上面的代码将输出:12 strp ...

  9. strlen函数,strcat函数,strcpy函数,strncpy函数,strcmp函数

    strcpy函数: char *strcpy(char *Dest , const char *Src) { assert((Dest != NULL) && (Src != NULL ...

最新文章

  1. java如何被调用_java – 如何知道Parse.initialize()是否已被调用?
  2. 神经网络相关名词解释
  3. android模拟器电量,Android 模拟器AVD,设置电池状态
  4. OpenMap教程第2部分–使用MapHandler构建基本地图应用程序–第1部分
  5. 当推荐系统遇见知识图谱会发生什么?
  6. [转载] Python使用list.reverse()返回None
  7. 赛尔译文 | 基础模型的机遇与风险 (四)
  8. keil_lic.exe注册机使用
  9. 快书编标让标书制作更高效、更规范、更轻松
  10. unity人物刚体移动_Unity3D 角色(物体) 移动方法 合集
  11. 社群编码识别黑灰产攻击实践
  12. 我的第一个“大工程”,欢迎来到m78星云
  13. 关于MATLAB直方图的绘制及应用
  14. 一文搞定高通量数据整合分析中批次效应的鉴定和处理
  15. Python采集喜马拉雅音频数据详解
  16. 用一个简单的例子来阐述强化学习的相关概念(二)
  17. 管理经济分析01:博弈论与经济学
  18. idea中用java不能自动导包的解决办法
  19. 辩证唯物论和唯物辩证法区别
  20. 解决span标签自带空格问题

热门文章

  1. linux 安装tomcat遇到的问题
  2. Effective Java之当心字符串连接的性能(五十一)
  3. hdu 1565 方格取数(1)
  4. 1050 String Subtraction (20 分)_10行代码AC
  5. 【最新合集】编译原理习题(含答案)_2程序设计语言及其文法_MOOC慕课 哈工大陈鄞
  6. [leetcode]541.反转字符串||
  7. 图片怎么压缩图片大小_图片的体积怎么压缩?这三种方法你会吗?
  8. 高效多用的群集-Haproxy搭建Web集群
  9. java枚举新特性_java回顾之枚举和新特性
  10. Windows + Eclipse + Gtk 环境(总结)