IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

空指针的解引用

kernelchina blogs 2012-09-06 23:46:40 累计浏览 3,310 次
本机暂存

    NULL指针的访问异常是C程序员常见的bug,事实上部分情况下空指针可以解引用,下面的thread,列出了几个例子。

    http://stackoverflow.com/questions/6793262/why-dereferencing-a-null-pointer-is-undefined-behaviour

    On the PDP-11, it happened that address 0 always contained the value 0, so dereferencing a null pointer also gave the value 0. Quite a few people who used these machines felt that since they were the original machine C had been written on/used to program, that this should be considered canonical behavior for C on all machines (even though it originally happened quite accidentally).

    On some other machines (Interdata comes to mind, though my memory could easily be wrong) address 0 was put to normal use, so it could contain other values.

    There was also some hardware on which address 0 was actually some memory-mapped hardware, so reading/writing it did special things -- not at all equivalent to reading/writing normal memory at all.

    空指针解引用是否导致异常应该是硬件设备和OS组合决定的。以前在VXwork下工作,空指针也可以解引用,可以访问内存0地址,还可以修改内容。这种情况下,为了便于程序员debug,印象中我们大概是采用了对于0地址内容监控,如果内容有改动则报告或者crash。

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. Java开发岗位面试题归类汇总 (累计阅读 22,155)
  2. Linux内存点滴 用户进程内存空间 (累计阅读 13,228)
  3. Linux Used内存到底哪里去了? (累计阅读 11,866)
  4. Linux操作系统的内存使用方法详细解析 (累计阅读 10,149)
  5. 几个内存相关面试题(c/c++) (累计阅读 9,443)
  6. top 命令补充 ( VIRT RES SHR) (累计阅读 9,115)
  7. Java程序员应该知道的10个eclipse调试技巧 (累计阅读 8,011)
  8. Innodb分表太多或者表分区太多,会导致内存耗尽而宕机 (累计阅读 7,717)
  9. redis 运维实际经验纪录之一 (累计阅读 7,711)
  10. 使用gdb调试运行时的程序小技巧 (累计阅读 7,206)