An Ubiquitous C bug

Norman Diamond diamond at jit345.swstokyo.dec.com
Mon Jan 21 19:31:06 AEST 1991


In article <s64421.664471332 at zeus> s64421 at zeus.usq.EDU.AU (house ron) writes:

>Here's a bug which exists in every single DOS C compiler I can find,
>and may also exist on others:
>In the small memory model, it is possible for a function to have the
>address NULL.  E.G.:
>void x() {}
>main() {
>   void (*y)() = x;
>   if (y==NULL) printf ("AARRGGHH!!\n");
>}
>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.

>Perhaps on some machines, NULL should _not_ be 0?

Of course.  On many many architectures, a bit pattern of all 1's would be
suitable and very very useful.
--
Norman Diamond       diamond at tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.



More information about the Comp.lang.c mailing list