From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,12b6bb94ebde41c X-Google-Attributes: gidfee84,public From: Steve VanDevender Subject: Re: Dereferencing NULL pointer? Date: 1997/05/21 Message-ID: #1/1 X-Deja-AN: 243560631 References: <5lteva$q0e@charnel.ecst.csuchico.edu> Organization: University of Oregon, Eugene Newsgroups: comp.unix.osf.osf1 tsaylor@ecst.csuchico.edu (Todd M. Saylor) writes: I am currently in the process of porting an application to Digital UNIX 4.0b (it's never been run on an alpha before). The app runs fine on HP/UX, AIX, DG/UX (motorola and Intel), AT&T/NCR. One problem has come up on previous ports, that is the ability to dereference a NULL pointer. For the OS's that hate this, we've always found a linker option to allow it (basically return a null character) - for example on DG it would be '-z lowzeros'. The following code demonstrates what I mean: main() { char *x; x = (char *)NULL; printf("Null string = '%s'\n", x); /* This works fine */ printf("Null Char = '%c'\n", *x); /* This get a seg. violation */ } Now this may be bad form, but I was wondering if anyone knows of a switch/option to allow it? TIA [Posted for Aaron Johnson / aaron@bi-tech.com] Your example program is inherently wrong. A NULL pointer in ANSI C is (void *) 0 -- it's valid to cast 0 to any pointer type to get a NULL, and it's also valid to cast void * to any other pointer type; while "#define NULL ((void *) 0)" is commonly used, it is no longer necessary to use NULL as an pointer constant when you can use 0 instead. A ASCII NUL character (note the different spelling) is '\0'. You don't get a NUL character by dereference of a NULL pointer, because dereference of a NULL pointer is undefined -- some architectures allow it but could return any value whatsoever, while others generate SIGSEGV if you do it, so you should never depend on it being possible in a portable program. You need to read a C book, and so did the authors of the program you are porting. In the long run you will be much better off fixing the program to eliminate the bogus NULL references than you are trying to find a way to make it allowable on the architectures you port the program to. I've been through such messes already and most such NULL reference problems introduce many subtle bugs. -- Steve VanDevender "I ride the big iron" stevev@efn.org stevev@hexadecimal.uoregon.edu http://jcomm.uoregon.edu/~stevev "bash awk grep perl sed df du, du-du du-du, vi troff su fsck rm * halt LART LART LART!" -- the Swedish BOFH