空指针的解引用
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。
建议继续学习:
- Linus:利用二级指针删除单向链表 (阅读:12812)
- C语言结构体里的成员数组和指针 (阅读:5925)
- 通过引用计数解决野指针的问题(C&C++) (阅读:4633)
- cpp智能指针的简单实现 (阅读:3951)
- C 语言中统一的函数指针 (阅读:4001)
- 重构发现:指针操作问题 (阅读:3378)
- 一起空指针引发的程序问题的排查过程 (阅读:2791)
- 关于类成员函数指针的正确写法 (阅读:2482)
扫一扫订阅我的微信号:IT技术博客大学习
- 作者:appleleaf 来源: kernelchina blogs
- 标签: 指针
- 发布时间:2012-09-06 23:46:40
-
[1188] WordPress插件开发 -- 在插件使用 -
[79] 解决 nginx 反向代理网页首尾出现神秘字 -
[43] web开发设计人员不可不用的在线web工具和 -
[36] Java开发岗位面试题归类汇总 -
[30] 手机产品设计方向 -
[30] Rax 系列教程(长列表) -
[29] 一句话crontab实现防ssh暴力破解 -
[27] 如何建立合适的索引? -
[27] 程序员疫苗:代码注入 -
[25] 我对学习oracle与成长的理解
