Referencing NULL pointers

John Sambrook john at amc-gw.UUCP
Tue Jul 11 11:43:11 AEST 1989


As several people have commented dereferencing NULL pointers is not an 
acceptable practice, even though you can get away with it on several 
architectures.  

There is another somewhat related issue, and that is that you have to 
be careful to make sure that the type of pointer you are dereferencing is
correct for the given usage.  For example, I have seen the following code 
from time to time:

    {
    	int fd;
    	struct stat s;
    	...

    	if (read(fd, &s, sizeof(struct stat)) != sizeof(struct stat))
    	...
    }

The problem with this code is that the second argument in the read()
call is of the wrong type; it should be of type "char *", yet in fact
it is of type "struct stat *".  

I spent several years working on a machine (Data General MV series) 
where this type of code would result in a segmentation violation when
it was executed.  Needless to say, I fixed a lot of bugs like this.
Fortunately the MV had a very good compiler, which made finding bugs
like this relatively easy.





-- 
John Sambrook                             DNS: john at amc.com
Applied Microsystems Corporation	 UUCP: amc-gw!john
Redmond, Washington  98073               Dial: (206) 882-2000 ext. 630



More information about the Comp.unix.wizards mailing list