五月26日是什么星座| 手脚发抖是什么原因引起的| 一什么千什么| 低血压是什么意思| 脂肪瘤吃什么药可以消除| 胶囊是什么原料做的| 2049年是什么年| 长期喝咖啡有什么好处和坏处| 葡式蛋挞为什么叫葡式| 可怜巴巴的意思是什么| 1月12日是什么星座| 卵巢囊性回声什么意思| daks是什么品牌| 镇团委书记是什么级别| 普惠性幼儿园是什么意思| 阿莫西林不能和什么药一起吃| 用进废退什么意思| 蜂蜜加白醋有什么功效| 白兰地兑什么饮料好喝| 男人阳萎吃什么药最好| 咳嗽胸口疼是什么原因| 孩子反复发烧是什么原因| 补钙什么季节补最好| 手指起水泡是什么原因| 肩周炎贴什么膏药效果最好| 着床出血是什么意思| 梅毒是什么样的| 冰箱冷藏室结冰是什么原因| 百合和什么搭配最好| 总是低烧是什么原因造成的| 阴囊湿疹是什么原因造成的| 肠胃功能紊乱什么症状| 女攻男受是什么意思| 胃烧心是什么感觉| 呆若木鸡的意思是什么| 汤力水是什么| browser什么意思| c3是什么| 淋巴结反应性增生是什么意思| 夏天脚出汗是什么原因| 眼睛痒用什么滴眼液| 看腋窝挂什么科| 男人吃香菜有什么好处| 肝内点状钙化灶什么意思| 7月22日是什么星座| 非甾体抗炎药是什么意思| 土豆可以做什么美食| 安厝是什么意思| 甲功三项是什么| 血药浓度是什么意思| 天秤座男生喜欢什么样的女生| 爸爸的哥哥叫什么| 血压是什么| 眼皮肿痛什么原因| 离婚需要什么手续和证件| 扫描件是什么意思| 尿隐血挂什么科| 冠心病做什么检查| 抽烟对女生有什么危害| 抽血化验挂什么科| 梦见把狗打死了是什么意思| 缩量是什么意思| 痛经吃什么药| 一般手脚慢进什么工厂| 月出念什么| 九牛一毛指什么生肖| 梦见鱼是什么预兆| 131是什么意思| 农历11月11日是什么星座| 嗜睡是什么病| 吞服是什么意思| 芥菜长什么样子图片| 高血压早餐吃什么好| 儿童抽动症挂什么科| 当我谈跑步时我谈些什么| 夏天煲什么汤| 为什么现在不建议输液| 雪花飘飘北风萧萧是什么歌| 01年的属什么| 70年出生属什么生肖| 什么酒适合女生喝| 50至60岁吃什么钙片好| 妇科菌群失调吃什么药| 走路气喘是什么原因| 背部疼痛是什么原因引起的| 狗鼻子为什么是湿的| h是什么牌子的衣服| 市委讲师团是什么级别| 打嗝不停是什么病前兆| 睡觉脚抽筋是什么原因引起的| 逍遥丸的功效和作用是什么| 黄金芽属于什么茶| 痉挛是什么| 胃糜烂吃什么药最好| 老鸨什么意思| 合肥有什么好玩的地方| 青茶是什么茶| 身上长了好多红痣是什么原因| 空腹不能吃什么水果| 前列腺有什么症状| 眼结石是什么原因引起的| 小便解不出来是什么原因| 鼻子冒热气是什么原因| 男人艾灸什么地方壮阳| 胸腔积液挂什么科| 吴亦凡属什么| 门字五行属什么| 嚣张是什么意思| 中性粒细胞百分比偏低是什么意思| 经常打飞机有什么危害| 绩效工资是什么| DDP是什么| 百岁山和景田什么关系| 给小孩办身份证需要什么| 眼睛充血是什么原因引起的| 经常尿路感染是什么原因| 执迷不悟是什么生肖| edv是什么意思| 支原体阳性是什么病| 绣眼鸟吃什么| 什么故事| 南极被称为什么| 血糖高饮食需要注意什么| 幽门螺杆菌是什么| 嫩绿的什么| 什么的蹦跳| 阴虚吃什么药效果最好| 6月6号是什么日子| 落魄是什么意思| 柿子不能跟什么一起吃| 鸟语花香是什么生肖| 什么月披星| 申时左眼跳是什么预兆| 医保自费是什么意思| 霖字五行属什么| 耍大牌是什么意思| 喝杨梅酒对身体有什么好处| 示数是什么意思| 洗衣机什么品牌好| 生抽和酱油有什么区别| 突然和忽然有什么区别| 阿胶适合什么人吃| 孕妇吃什么好| 弟妹是什么意思| 肝硬化吃什么水果好| 胸口疼痛吃什么药| 99年属什么生肖| 干涸是什么意思| 女性肠痉挛有什么症状| 胎儿左心室灶状强回声是什么意思| 胎盘有什么用| 智齿什么时候开始长| 银河系的中心是什么| 三七粉是治什么病的| 发量少适合什么发型| 上梁不正下梁歪是什么意思| 真菌孢子阳性什么意思| 积食吃什么食物帮助消化| 丰富是什么意思| 三点水加四读什么| 土猪肉和普通猪肉有什么分别| 立夏吃什么蛋| 性欲什么意思| 热伤风吃什么药好得快| 做爱吃什么药| 胆红素高挂什么科检查| 冬瓜与什么食物相克| ldpe是什么材料| 成吉思汗姓什么| 魔改是什么意思| 外阴瘙痒抹什么药| 75年属什么生肖| 处长什么级别| 黄帝叫什么名字| 50年属什么| 睾丸是什么东西| 吃无花果有什么好处| th什么意思| 梦见被雷劈什么意思| 内向男生适合什么工作| 出水痘不能吃什么食物| 琥珀酱是什么味| 刘庄为什么要灭了阴家| 什么是特异性皮炎| 睡眠不好吃什么好| 关帝庙求什么最灵| 晕车药什么时候吃最好| 泡热水脚有什么好处| 白细胞低吃什么| 北极贝长什么样| 为什么白天尿少晚上尿多| 狗狗为什么会咬人| 245是什么意思| 溪字五行属什么| 癔病是一种什么病| 鸡咳嗽吃什么药| 三月二十二是什么星座| 狗屎运是什么意思| 记忆力差是什么原因| 运气是什么意思| 知见是什么意思| 偏执是什么意思| 七月十三日是什么日子| 葳是什么意思| 马黛茶什么味道| 慢性浅表性胃炎伴糜烂吃什么药| 耳朵里面痒是什么原因| 梅花是什么颜色的| 睡觉磨牙齿是什么原因| 手术后拆线挂什么科| 世界八大奇迹分别是什么| 什么血型和什么血型不能生孩子| 幽门螺旋杆菌是什么意思| 虬结什么意思| 痛什么什么痛| roi是什么| 梦见很多棺材是什么征兆| 牙齿突然酸痛什么原因| 睾丸是什么形状的| egm是什么意思| 4月7号是什么星座| 什么是平舌音什么是翘舌音| 扇贝不能和什么一起吃| 右侧卵巢内囊性回声是什么意思| 粘纤是什么面料| 双侧胸膜局限性增厚是什么意思| 白泽长什么样| 1954年是什么年| 麻瓜是什么意思| 落花生为什么叫落花生| 上面一个日下面一个立是什么字| 咳嗽有白痰是什么原因| 月经量特别少是什么原因| 造影有什么危害| 金融办是什么单位| 老年脑改变是什么意思| 例假是什么| 一什么杯子| 豹子号是什么意思| 指南针什么时候发明的| 感冒流黄鼻涕吃什么药| 什么是全运会| 房颤是什么症状| 高危型hpv52阳性是什么意思| 甲申日五行属什么| 肚子疼什么原因| 脉搏细是什么原因| 梦见盖房子是什么预兆| 黑京念什么| 类固醇是什么东西| 破财消灾什么意思| 吃什么白蛋白提升快| 螺蛳吃什么| 纯化水是什么水| n什么意思| 木甚念什么| 凌晨四点醒是什么原因| 豆包是什么| 人格魅力什么意思| 妊娠是什么意思啊| 孩子不长个子是什么原因| 男人阴茎硬不起来是什么原因| 7月4号是什么星座| 全身发痒是什么原因| 百度Jump to content

车里总是弥漫着汽油味 不怪车怪自己

From Wikipedia, the free encyclopedia
百度 (责编:张歌、白宇)

In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.

A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, in general, most languages do not offer such guarantee for uninitialized pointers. It might compare equal to other, valid pointers; or it might compare equal to null pointers. It might do both at different times; or the comparison might be undefined behavior. Also, in languages offering such support, the correct use depends on the individual experience of each developer and linter tools. Even when used properly, null pointers are semantically incomplete, since they do not offer the possibility to express the difference between "not applicable", "not known", and "future" values.[citation needed]

Because a null pointer does not point to a meaningful object, an attempt to access the data stored at that (invalid) memory location may cause a run-time error or immediate program crash. This is the null pointer error, or null pointer exception. It is one of the most common types of software weaknesses,[1] and Tony Hoare, who introduced the concept, has referred to it as a "billion dollar mistake".[2]

C

[edit]

In C, two null pointers of any type are guaranteed to compare equal.[3] Prior to C23, the preprocessor macro NULL was provided, defined as an implementation-defined null pointer constant in <stdlib.h>,[4] which in C99 can be portably expressed as ((void *)0), the integer value 0 converted to the type void* (see pointer to void type).[5] Since C23, a null pointer is represented with nullptr which is of type nullptr_t (originally introduced to C++11), providing a type safe null pointer.[6]

The C standard does not say that the null pointer is the same as the pointer to memory address 0, though that may be the case in practice. Dereferencing a null pointer is undefined behavior in C,[7] and a conforming implementation is allowed to assume that any pointer that is dereferenced is not null.

In practice, dereferencing a null pointer may result in an attempted read or write from memory that is not mapped, triggering a segmentation fault or memory access violation. This may manifest itself as a program crash, or be transformed into a software exception that can be caught by program code. There are, however, certain circumstances where this is not the case. For example, in x86 real mode, the address 0000:0000 is readable and also usually writable, and dereferencing a pointer to that address is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behavior in the application; if a null pointer is represented as a pointer to that address, dereferencing it will lead to that behavior. There are occasions when dereferencing a pointer to address zero is intentional and well-defined; for example, BIOS code written in C for 16-bit real-mode x86 devices may write the interrupt descriptor table (IDT) at physical address 0 of the machine by dereferencing a pointer with the same value as a null pointer for writing. It is also possible for the compiler to optimize away the null pointer dereference, avoiding a segmentation fault but causing other undesired behavior.[8]

C++

[edit]

In C++, while the NULL macro was inherited from C, the integer literal for zero has been traditionally preferred to represent a null pointer constant.[9] However, C++11 introduced the explicit null pointer constant nullptr and type nullptr_t to be used instead, providing a type safe null pointer. nullptr and type nullptr_t were later introduced to C in C23.

Other languages

[edit]

In some programming language environments (at least one proprietary Lisp implementation, for example),[citation needed] the value used as the null pointer (called nil in Lisp) may actually be a pointer to a block of internal data useful to the implementation (but not explicitly reachable from user programs), thus allowing the same register to be used as a useful constant and a quick way of accessing implementation internals. This is known as the nil vector.

In languages with a tagged architecture, a possibly null pointer can be replaced with a tagged union which enforces explicit handling of the exceptional case; in fact, a possibly null pointer can be seen as a tagged pointer with a computed tag.

Programming languages use different literals for the null pointer. In Python, for example, a null value is called None. In Java and C#, the literal null is provided as a literal for reference types. In Pascal and Swift, a null pointer is called nil. In Eiffel, it is called a void reference.

Null dereferencing

[edit]

Because a null pointer does not point to a meaningful object, an attempt to dereference (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash. MITRE lists the null pointer error as one of the most commonly exploited software weaknesses.[10]

  • In C, dereferencing a null pointer is undefined behavior.[7] Many implementations cause such code to result in the program being halted with an access violation, because the null pointer representation is chosen to be an address that is never allocated by the system for storing objects. However, this behavior is not universal. It is also not guaranteed, since compilers are permitted to optimize programs under the assumption that they are free of undefined behavior. This behaviour is the same in C++, as there is no null pointer exception in the C++ language. On platforms such as Unix-like systems and Windows with the Visual Studio compiler, an access violation causes a C/C++ SIGSEGV signal to be issued. Although in C/C++ null dereferences are not exceptions which can be caught in C++ try/catch blocks, it is possible to "catch" such an access violation by using (std::)signal() in C/C++ to specify a handler to be called when that signal is issued.
  • In D, much like C++, a null pointer dereference results in a segmentation fault.
  • In Delphi and many other Pascal implementations, the constant nil represents a null pointer to the first address in memory which is also used to initialize managed variables. Dereferencing it raises an external OS exception which is mapped onto a Pascal EAccessViolation exception instance if the System.SysUtils unit is linked in the uses clause.
  • In Java, access to a null reference (null) causes a NullPointerException (NPE), which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur.
  • In Lisp, nil is a first class object. By convention, (first nil) is nil, as is (rest nil). So dereferencing nil in these contexts will not cause an error, but poorly written code can get into an infinite loop.
  • In .NET and C#, access to null reference (null) causes a NullReferenceException to be thrown. Although catching these is generally considered bad practice, this exception type can be caught and handled by the program.
  • In Objective-C, messages may be sent to a nil object (which is a null pointer) without causing the program to be interrupted; the message is simply ignored, and the return value (if any) is nil or 0, depending on the type.[11]
  • In Python, dereferencing None raises an AttributeError, as NoneType has no (user-facing) methods or attributes defined.
  • In Rust, dereferencing a null pointer (std::ptr::null()) in an unsafe block results in undefined behaviour, which usually results in a segmentation fault or corrupted memory.
  • Before the introduction of Supervisor Mode Access Prevention (SMAP), a null pointer dereference bug could be exploited by mapping page zero into the attacker's address space and hence causing the null pointer to point to that region. This could lead to code execution in some cases.[12]

Mitigation

[edit]

While there could be languages with no nulls, most do have the possibility of nulls so there are techniques to avoid or aid debugging null pointer dereferences.[13] Bond et al.[13] suggest modifying the Java Virtual Machine (JVM) to keep track of null propagation.

There are three levels of handling null references, in order of effectiveness:

  1. languages with no null;
  2. languages that can statically analyse code to avoid the possibility of null dereference at run time;
  3. if null dereference can occur at runtime, tools that aid debugging.

Pure functional languages are an example of level 1 since no direct access is provided to pointers and all code and data is immutable. User code running in interpreted or virtual-machine languages generally does not suffer the problem of null pointer dereferencing.[dubiousdiscuss]

Where a language does provide or utilise pointers which could become void, it is possible to avoid runtime null dereferences by providing compilation-time checking via static analysis or other techniques, with syntactic assistance from language features such as those seen in the Eiffel programming language with Void safety[14] to avoid null dereferences, D,[15] and Rust.[16]

In some languages analysis can be performed using external tools, but these are weak compared to direct language support with compiler checks since they are limited by the language definition itself.

The last resort of level 3 is when a null reference occurs at runtime, debugging aids can help.

History

[edit]

In 2009, Tony Hoare stated[2][17] [18] that he invented the null reference in 1965 as part of the ALGOL W language. In that 2009 reference Hoare describes his invention as a "billion-dollar mistake":

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

See also

[edit]

Notes

[edit]
  1. ^ "CWE-476: NULL Pointer Dereference". MITRE.
  2. ^ a b Tony Hoare (2025-08-06). "Null References: The Billion Dollar Mistake". InfoQ.com.
  3. ^ ISO/IEC 9899, clause 6.3.2.3, paragraph 4.
  4. ^ ISO/IEC 9899, clause 7.17, paragraph 3: NULL... which expands to an implementation-defined null pointer constant...
  5. ^ ISO/IEC 9899, clause 6.3.2.3, paragraph 3.
  6. ^ "WR14-N3042: Introduce the nullptr constant". open-std.org. 2025-08-06. Archived from the original on December 24, 2022.
  7. ^ a b ISO/IEC 9899, clause 6.5.3.2, paragraph 4, esp. footnote 87.
  8. ^ Lattner, Chris (2025-08-06). "What Every C Programmer Should Know About Undefined Behavior #1/3". blog.llvm.org. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  9. ^ Stroustrup, Bjarne (March 2001). "Chapter 5:
    The const qualifier (§5.4) prevents accidental redefinition of NULL and ensures that NULL can be used where a constant is required.". The C++ Programming Language (14th printing of 3rd ed.). United States and Canada: Addison–Wesley. p. 88. ISBN 0-201-88954-4.
  10. ^ "CWE-476: NULL Pointer Dereference". MITRE.
  11. ^ The Objective-C 2.0 Programming Language, section "Sending Messages to nil".
  12. ^ "OS X exploitable kernel NULL pointer dereference in AppleGraphicsDeviceControl"
  13. ^ a b Bond, Michael D.; Nethercote, Nicholas; Kent, Stephen W.; Guyer, Samuel Z.; McKinley, Kathryn S. (2007). "Tracking bad apples". Proceedings of the 22nd annual ACM SIGPLAN conference on Object oriented programming systems and applications - OOPSLA '07. p. 405. doi:10.1145/1297027.1297057. ISBN 9781595937865. S2CID 2832749.
  14. ^ "Void-safety: Background, definition, and tools". Retrieved 2025-08-06.
  15. ^ Bartosz Milewski. "SafeD – D Programming Language". Retrieved 17 July 2014.
  16. ^ "Fearless Security: Memory Safety". Archived from the original on 8 November 2020. Retrieved 4 November 2020.
  17. ^ Tony Hoare (2025-08-06). "Presentation: "Null References: The Billion Dollar Mistake"". InfoQ.com.
  18. ^ Contieri, Maxi (2025-08-06). "Code Smell 304 - Null Pointer Exception". Maximiliano Contieri - Software Design. Retrieved 2025-08-06.

References

[edit]
  • Joint Technical Committee ISO/IEC JTC 1, Subcommittee SC 22, Working Group WG 14 (2025-08-06). International Standard ISO/IEC 9899 (PDF) (Committee Draft).{{cite book}}: CS1 maint: multiple names: authors list (link) CS1 maint: numeric names: authors list (link)
女生有喉结是什么原因 3.8号是什么星座 lg什么意思 一什么云彩 眉眼是什么意思
结缔组织病是什么病能治愈吗 梦见衣服是什么意思 净字五行属什么 男生生日送什么礼物好 为什么手机打不出去电话
一切就绪是什么意思 早上起来手麻是什么原因 真正的朋友是什么 梦见吃西红柿是什么意思 治疗阳痿早泄什么药最好
12月13号是什么星座 hoka跑鞋中文叫什么 下火喝什么茶 女孩名字带什么字好听 afar是什么意思
dht是什么hcv7jop6ns6r.cn 过敏性咳嗽吃什么药好hcv9jop4ns9r.cn 什么颜色属土hcv8jop0ns9r.cn 时迁的绰号是什么hcv9jop4ns3r.cn 门可罗雀是什么意思hcv8jop1ns8r.cn
好学不倦什么意思hcv8jop9ns9r.cn 弱碱水是什么水hcv7jop4ns6r.cn 冰妹是什么xinmaowt.com 急支糖浆是什么梗hcv9jop7ns1r.cn 无缘是什么意思hcv8jop6ns8r.cn
上海市委书记什么级别hcv8jop6ns2r.cn 狗皮肤溃烂用什么药hcv8jop6ns7r.cn 女人辟邪带什么最好hcv9jop1ns1r.cn 胸闷气短呼吸困难心慌是什么原因hcv7jop7ns3r.cn 安属于五行属什么hcv9jop8ns2r.cn
属蛇和什么属相相冲hcv9jop0ns0r.cn 肠结核是什么病hcv8jop9ns9r.cn 马齿苋什么人不能吃hcv7jop6ns0r.cn 湛蓝湛蓝的什么hcv8jop6ns9r.cn 岚的意思是什么hcv8jop7ns2r.cn
百度