Bryan said:
Walter said:
Writing through a null pointer is no more defined than writing
through a
wild pointer.
Maybe, but an error is more or less guaranteed with a null pointer and
makes it easy to check for.
I know of at least two architectures in which NULL is all-zero-bits
but in which writing at (or near that location, such as a[3] when
a is NULL) would NOT result in a trap, because the architectures happen
to have memory at that location.
The vast majority of machines in use have NULL as a trap location.
Richard's
development system likely does so as well. Although *NULL might well not
trap, it's in general more likely to, which can help with debugging.
This will not catch all such errors of course, but it doesn't hurt
anything,
except for causing a tiny (probably imperceptible) performance hit,
and may
help with debugging.
Agreed.
I would also say that even on systems where writing to *NULL does not
trap, it is still likely (but not guaranteed) to give reproduceable
results which can be debugged.
For similar reasons on embedded systems I have changed the startup code
so that all RAM was initialised to all bits 0. I did not check on the
system whether NULL was all bits 0 or whether float or double 0 where
all bits 0, but it did meen the software was more predictable and easier
to debug when accessing uninitialised data. On another system they
initialised all RAM to 0x55 for the similar reason (I used all bits 0
because static variables without an initialiser where *not* set to 0).
IMHO the first stage in trying to debug a problem is always to reproduce
it, since if you can't reliably reproduce it you will never know if you
have fixed what caused the original bug report or something else that
you think might have caused it but did not.