Fixing C's Biggest Mistake

Walter Bright newshound2 at digitalmars.com
Fri Dec 30 20:27:43 UTC 2022


On 12/29/2022 8:01 PM, Timon Gehr wrote:
 > Even array bounds overflow exceptions would be better as compile-time errors. 
If you don't consider that practical, that's fine, I guess it will take a couple 
of decades before people accept that this is a good idea,

The size of the array depends on the environment. I don't see how to do that at 
compile time.

 > but it's certainly practical today for null dereferences.

Pattern matching inserts an explicit runtime check, rather than using the 
hardware memory protection to do the check. All you get with pattern matching is 
(probably) a better error message, and a slower program. You still get a fatal 
error, if the pattern match arm for the null pointer is fatal.

You can also get a better error message with a seg fault if you code a trap for 
that error.

Isn't it great that the hardware provides runtime null checking for you at zero 
cost?

If a seg fault resulted in memory corruption, then I agree with you. But it 
doesn't, it's at zero cost, your program runs at full speed.

P.S. in the bad old DOS days, a null pointer write would scramble DOS's 
interrupt table, which had unpredictable and often terrible effects. 
Fortunately, uP's have evolved since then into having hardware memory 
protection, so that is no longer an issue. As soon as I got a machine with 
memory protection, I switched all my development to that. Only as a last step 
did I recompile it for DOS.



More information about the Digitalmars-d mailing list