Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!snorkelwacker!bloom-beacon!ora!minya!jc From: jc@minya.UUCP (John Chambers) Newsgroups: comp.unix.wizards Subject: Re: What machines core dump on deref of NULL? Summary: Dereferencing a NULL pointer is *not* an error. Message-ID: <412@minya.UUCP> Date: 29 Jun 90 11:13:27 GMT References: <444@mtndew.UUCP> <31079@cup.portal.com> <13226@smoke.BRL.MIL> Lines: 57 In article <13226@smoke.BRL.MIL>, gwyn@smoke.BRL.MIL (Doug Gwyn) writes: > In article <2389@aix.aix.kingston.ibm.com> rlc@aix.aix.kingston.ibm.com (Roger Collins) writes: > >In article <31079@cup.portal.com> thad@cup.portal.com (Thad P Floryan) writes: > >> Point being: there "may" be some method (perhaps under software control) to > >> disable the MMU on YOUR system to catch NULL dereferencing... > >First, the way that the systems causes null pointer dereferencing > >*not* to fail is so simple that all systems should do it: just make > >sure address 0 (page 0) is allocated and set to 0. > > To the contrary, this merely prevents genuine bugs from being caught > as soon as they would be were dereference of a null pointer to trap. > Dereferencing a null pointer is a serious BUG in any application and > can indicate an algorithmic error that should be tracked down before > it is too late. Hey, wait just a minute here. I can't let such an erroneous error go unchallenged! Dereferencing a null pointer is quite definitely *not* an error, bug, mistake, or any other pejorative, in a great many sorts of applications. The trouble with generalizing to all C code is that C outgrew Unix about a decade ago. I've written many programs that are to be downloaded and run all by their lonesome in an "embedded" processor. Lots of people use C for such applications, and there are a *lot* of processors (in fact, the overwhelming majority of them) that run only one program (without an operating system) for their entire life. Such embedded, standalone programs not only can, but are required to access all of physical memory, including address zero. The hardware says that something particular resides at the bottom end of memory, and such a program must access that byte/word/whatever, or it can't do its job. If a C compiler won't let me access that address, then I look for another that was written by someone more competent. Now, if you were to suggest an *optional* feature that would allow me to turn null-pointer checking off and on (preferably at run time, but a compile-time flag would be useful), I'd grab it and use it. After all, it is pretty much true that when a process running on a Unix system dereferences a null pointer, it is generally a bug. Of course, all C compilers already have a perfectly good tool for doing a run-time check for a null pointer: if (p == 0 ) ... I use it a lot. In particular, I introduce it all over the place in code that I'm porting from VAXen to other machines. I suggest that others try it. It's a whole lot nicer than having your program bomb out on a SEGV. (Now if there were only an equivalent piece of code that would tell me whether a non-null pointer is valid.) -- Uucp: ...!{harvard.edu,ima.com,eddie.mit.edu,ora.com}!minya!jc (John Chambers) Home: 1-617-484-6393 Work: 1-508-952-3274 Cute-Saying: [I've gotta get a new one of these some day.]