views:

217

answers:

3

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.

A: 

When you say C like, it goes to same direction as others. Scala somehow eliminates nulls using option cases. Still, you can use nulls. Functinal languages have no null problems and side effects but they are not c like. Dynamic languages also have nulls or undefined variables and such. It is a price you need yo pay for c like imho.

Ekin Koc
+1  A: 

There is a reason why it was not replaced, look at it this way, there is such a huge wide base of C programs, thousands upon thousands of code lying around, to create a replacement would cause a lot of grief as the code would have to be updated to reflect the change, as a NULL in the context of a pointer means that the call would have failed and hence return a NULL to signify the condition that the code to deal with the pointer has failed. Also, it is used in the case of FILE * operations, if the file was invalid, NULL would be returned. Imagine the hassle if all those code would be changed, unfortunately, it is part of the ANSI C Standard and therefore a very costly exercise in terms of $$$ to get all this changed to adapt to a new convention for a NULL-replacement.

Have a look at this FAQ on the infamous NULL pointer as found in the C FAQ.

Edit: By the way, 'I get that null terminated strings, arrays with no length and a few other things are bad' That is a wrong way of saying it as you are confusing the two...as Peter Van Der Linden author of 'Expert C Programming - Deep C Secrets', a nul with one l is a '\0', a null with two l is a NULL! No such thing as null terminated, that should have read as nul terminated i.e. '\0' which is a placeholder to signify end of string.

Hope this helps, Best regards, Tom.

tommieb75
+1 for the nul / NULL difference.
JXG
+1  A: 

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.

Peter Eisentraut