4 ms·
> dereferencing a null pointer results in undefined behaviour, which is often tough to deal with. Having an exception thrown is a lot nicer. Yes, it's undefine
by WalterBright 7y ago
> dereferencing a null pointer results in undefined behaviour, which is often tough to deal with. Having an exception thrown is a lot nicer.
Yes, it's undefined behavior, but in reality you get a null pointer seg fault on systems with memory protection.
- asveikau 7y agoThis is indeed how certain language runtimes generate null pointer exceptions. It would be too slow to add comparisons against null to every use of a pointer. So they handle SIGSEGV and raise the exception if the faulting access is on the null page. Interesting side question: if you have a struct bigger than a page, and you dereference the end of it... How big does it have to be to not get this behavior?
- WalterBright 7y agoThat's a great question. The best way to find out is to write a program that pokes every 4k until you find the boundary.
- TheCoelacanth 7y agoNot in cases where the optimizer did something tricky based on the assumption that null pointer dereferences never happen (which is allowed because they are undefined behavior so any behavior is correct).
- WalterBright 7y agoI'm aware of those, but don't implement them in my compilers. It's not worth it.
- ktm5j 7y agoOh wow, I didn't know you were on here! I'm a huge fan of D so I just wanted to say thanks for all the work you've done. D is an absolute pleasure to work with.
- WalterBright 7y agoThanks for the kind words!
- renox 7y agoYou didn't but I remember a case where Linux's devs got bitten by gcc which did it..
- Athas 7y agoThis does not save you if that null pointer is supposed to point to an array, and subsequent index operations move it out of the protected first page (or the first couple, I forget how modern Unix does it).