Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!casbah.acns.nwu.edu!hpa From: hpa@casbah.acns.nwu.edu (Peter Anvin) Newsgroups: comp.lang.c Subject: Re: An Ubiquitous C bug Message-ID: <2831@casbah.acns.nwu.edu> Date: 21 Jan 91 18:30:23 GMT References: <1991Jan21.083106.7297@tkou02.enet.dec.com> Organization: Northwestern University Lines: 31 [Program comparing a function pointer to NULL deleted] >>This sort of program CAN print the message if x() happens to be >>loaded by the linker at the start of the code segment (address 0). > >According to section 3.2.2.3: "An integral constant expression with the >value 0, or such an expression cast to type void *, is called a null pointer >constant. If a null pointer constant is assigned to or compared for equality >to a pointer, the constant is converted to a pointer of that type. Such a >pointer, called a null pointer, is guaranteed to compare unequal to a pointer >to any object type or function." >Every single DOS compiler that you can find is non standard-conforming. All DOS compilers I know of use, in small-code models, the beginning of the code segment to store their startup code. Thus, functions cannot be allocated to CS:0000h, and thus no functions correspond to NULL. The startup cod, which is written in assembler, sets up the stack, local heap, floating point emulation and the arguments to main(), as appropriate. main() just acts like a subroutine to the startup code. Should NULL be all ones? Performance issues aside, such a machine would only need to subtract one when converting an int to a pointer, and add one the other way. In constant expressions, such as when using the macro NULL, that can of course be done at compile time. -- H. Peter Anvin +++ A Strange Stranger +++ N9ITP/SM4TKN +++ INTERNET: hpa@casbah.acns.nwu.edu FIDONET: 1:115/989.4 BITNET: HPA@NUACC RBBSNET: 8:970/101.4