没有NULL的类C语言? [英] C-like language without NULL?

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

问题描述

您好,我最近正在观看旧的视频,介绍空指针是如何造成10亿美元错误的.他指出C#和Java都具有运行时检查功能,但是并没有完全消除它,这在一定程度上是可以理解的.他还指出了C,他认为如此肯定是一个很大的问题.我得到了以null结尾的字符串,没有长度的数组以及其他一些不好的东西(缓冲区溢出漏洞数十亿美元),但是要完全删除null?

某些语言已经使null成为过去,其他语言试图替代C,但是我找不到哪种语言可以同时实现这两种语言.

如果C的null非常糟糕,那么为什么没有人创建它来替代它呢?即Haskell很好,但不能作为替代产品.

解决方案

在C中使用空指针的问题是,当您引用它们时,程序将以未定义的方式运行,从而导致无数的错误,漏洞和系统"崩溃"演示文稿中提到.但这并不意味着非真实价值的想法一定是不好的.许多语言都包含它们.它们被称为NULL,None,NaN,undef,null,nil等.您只需要一种定义明确且健壮的方法来处理它们.

我想为什么像C但不会在空指针解引用时崩溃"这样的语言尚未得到广泛采用的原因是,这种语言的利基很小.一方面,您需要进行系统编程,其中(据称)您需要对正在发生的一切进行严格控制,并且无法让编译器在每次取消引用指针时都插入自动空指针检查.另一方面,存在应用程序和其他对性能要求不高的编程,在其中进行上一层或多层抽象更容易,而完全不用考虑指针,这很容易导致Java,C#,Python等问题.

Hi I was recently watching an old video about how null pointers were a billion dollar mistake. He points out both C# and java as they have run-time checks but don't completely eliminate it enough and this is somewhat understandable. He also points out the C at one point which he feels so sure of is a great problem. I get that null terminated strings, arrays with no length and a few other things are bad (billions of dollars on buffer overflow exploits) but to completely remove null?

Some languages have made null a thing of the past and other languages have attempted at being a replacement for C but I cannot find which language has accomplished both.

If C's null is so bad then why hasn't anyone created it's replacement? i.e. Haskell is nice but cannot operate as a drop in replacement.

解决方案

The problem with null pointers in C is that when you reference them, the program behaves in undefined ways, which results in the "innumerable errors, vulnerabilities, and system crashes" the presentation mentions. But that doesn't mean the idea of a value that is not a real value is necessarily bad. Many languages contain them; they are called NULL, None, NaN, undef, null, nil, etc. You just need to have a well-defined and robust way to deal with them.

I suppose the reason why a language "like C but doesn't crash on null pointer dereferencing" has not seen wide adoption is that the niche for such a language would be very small. One the one hand, you have systems programming where you (purportedly) need tight control over everything that is going on and cannot afford the compiler inserting an automatic null pointer check at every pointer dereference. On the other hand, there is application and other less performance critical programming, where it's much easier to move up a layer or more of abstraction and forget about pointers altogether, which results in things like Java, C#, Python, and others.

这篇关于没有NULL的类C语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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