[comp.os.minix] stdio.h : NULL

UPSYF173%DBIUNI11.BITNET@cunyvm.cuny.edu (Wolfgang Thiel) (10/02/90)

     Hi,
         #ifdef NULL
         #undef NULL
         #endif
         ...
         #define NULL 0
This really is bad!! It took me more than 2 hours with mdb to find out why
the program crashed. Of course, my machine's pointer size is not
sizeof int. Doesn't   #define NULL  ((void *)0)  do the job an any machine?
Or do I miss something?
                             Wolfgang Thiel

andrew@eleceng.bradford.ac.uk (Andrew G. Minter) (10/02/90)

>          #ifdef NULL
>          #undef NULL
>          #endif
>          ...
>          #define NULL 0
> This really is bad!! It took me more than 2 hours with mdb to find out why
> the program crashed. Of course, my machine's pointer size is not
> sizeof int. Doesn't   #define NULL  ((void *)0)  do the job an any machine?
> Or do I miss something?

This one caught me to.  NULL is defined as you suggest in a number of other
file in /usr/include, but not in stdio.h!  The very fact that it is defined
in so many places can cause problems when using Bruce Evans' C compiler,
which protests when NULL is redefined.

There seem to be quite a few fixes to small problems of this ilk floating
around the net.  Is anyone planning to collect them and introduce an
official (or even unofficial) 1.6?  We may have to wait a long time before
there is any sign of 2.0.

How would AST feel about some kind of unofficial user group version of
Minix?

Cheers, Andrew
-- 
##############################################################################
# Andrew G. Minter,                  #  JANET: andrew@bradford               #
# Lecturer in Information Systems,   #  EARN/BITNET: andrew@bradford.ac.uk   # 
# Dept. of Electrical Engineering,   #                                       #
# University of Bradford,            #  Phone: +44.274.733466 ext 347 or 326 #
# Bradford, West Yorkshire, BD7 1DP  #  Telex: 51309 UNIBFD G                #
# United Kingdom                     #  Fax: +44.274.391521 <-- just changed #
##############################################################################
#       Soon moving to STC Technology Ltd, London Road, Harlow, Essex        #
##############################################################################

jenkins@jpl-devvax.JPL.NASA.GOV (Steve Jenkins) (10/02/90)

In article <32108@nigel.ee.udel.edu> UPSYF173%DBIUNI11.BITNET@cunyvm.cuny.edu (Wolfgang Thiel) writes:
>[...]
>         #define NULL 0
>This really is bad!! [....] Of course, my machine's pointer size is not
>sizeof int. Doesn't   #define NULL  ((void *)0)  do the job an any machine?

In C, the integer constant 0 can always be used to represent a nil
pointer.  So "#define NULL 0" is correct.  The relative sizes of
pointers and ints are irrelevant, as is the internal representation of
the nil pointer.

The constant 0 can be used uncast in any pointer context.  It's the
compiler's job to infer from the context that it's a nil pointer.
One common case where the compiler can't tell is in actual function
arguments where a prototype is not in scope.  In this case the
compiler rightfully treats 0 as an integer, and pain follows.  In
these cases you should use a prototype (if your compiler supports
them) or cast 0 to the appropriate pointer type.

(void *) 0 is allowable, but unnecessary.  It doesn't fix the general
problem;  without a prototype or cast, it will fail similarly on
machines with more than one size of pointer.

See the Frequently-Asked-Questions list in comp.lang.c for the
distillation of *years* of discussion of this problem.

-- 
Steve Jenkins N6UNI			jenkins@jpl-devvax.jpl.nasa.gov
Caltech/Jet Propulsion Laboratory	(818) 354-0162

sjo@cci632.UUCP (Steve Owens) (10/03/90)

In article <32115@nigel.ee.udel.edu>, andrew@eleceng.bradford.ac.uk (Andrew G. Minter) writes:
	
	[ Lines about #define NULL problems deleted....]

> There seem to be quite a few fixes to small problems of this ilk floating
> around the net.  Is anyone planning to collect them and introduce an
> official (or even unofficial) 1.6?  We may have to wait a long time before
> there is any sign of 2.0.

	What about a patch disk from PH?  It could then be included in the
standard packages from PH, while those of us willing to spend the cost of
a postage stamp could get the fixes.

	Of course, it would be *nice* to recieve them over the net for free,
but I'm sure PH might complain rather loudly about copyrights, etc.

	Oh, well.

> Cheers, Andrew


				SJO

drd@siia.mv.com (David Dick) (10/26/90)

In <32108@nigel.ee.udel.edu> UPSYF173%DBIUNI11.BITNET@cunyvm.cuny.edu (Wolfgang Thiel) writes:


>     Hi,
>         #ifdef NULL
>         #undef NULL
>         #endif
>         ...
>         #define NULL 0
>This really is bad!! It took me more than 2 hours with mdb to find out why
>the program crashed. Of course, my machine's pointer size is not
>sizeof int. Doesn't   #define NULL  ((void *)0)  do the job an any machine?
>Or do I miss something?
>                             Wolfgang Thiel

First, "void *" is not available on all machines.  Second,
0 is legal C for "null pointer" in *all* cases except when
a pointer is passed as an argument to a function.  If this
is not true for a particular compiler, the compiler is broken.

David Dick
Software Innovations, Inc. [the Software Moving Company (sm)]