null dereference exception vs. segfault?

Jonathan M Davis jmdavisprog at gmail.com
Mon Aug 2 12:01:19 PDT 2010


On Monday, August 02, 2010 08:34:50 Jeffrey Yasskin wrote:
> That's good to know. Unfortunately, reading through a null pointer
> does cause undefined behavior: it's not a guaranteed segfault.
> Consider an object with a large array at the beginning, which pushes
> later members past the empty pages at the beginning of the address
> space. I don't suppose the D compiler watches for such large objects
> and emits actual null checks before indexing into them?

There are no null checks. When people have requested in the past that null 
checks be added (like you'd get in Java), Walter has indicated that he thought 
that there was no point to them because the OS takes care of them already by 
giving you a segfault. I'm not personally well-versed enough in exactly what 
goes on at the hardware or OS level to produce a segfault, so I can't say 
whether a segfault is absolutely guaranteed. It has been my understanding that 
it is.

As for indexing into an array, the array itself should be null or not. It has no 
size if it's null, so it makes no sense to talk about large arrays which are 
null. On top of that, bounds checking is usually done on arrays (off of the top 
of my head, I don't remember the exact circumstances under which it's removed, 
but it's almost always there), so you wouldn't be able to index past its end, 
and if it's an element of the array that you're dereferencing, then whether that 
element is null or not will determine whether it segfaults.

> > The pages that you're looking at there need to be updated for clarity.
> 
> Nice use of the passive voice. Who needs to update them? Is their
> source somewhere you or I could send a patch?

Submit a bug report to bugzilla: http://d.puremagic.com/issues/

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list