[PATCH] Fixes sentinel warnings on GCC4

Chris Shoemaker c.shoemaker at cox.net
Tue Dec 6 16:24:16 EST 2005


On Tue, Dec 06, 2005 at 08:43:21AM -0800, Karl Hegbloom wrote:
> On Mon, 2005-12-05 at 21:25 -0500, Chris Shoemaker wrote:
> > If NULL is ((void*)0) you won't have any problem using NULL as a
> > sentinel, but using that definition of NULL opens up the possibility
> > of writing code that works with one implementation and not with
> > another.  Specifically, you couldn't say "int *i = ((int *) NULL)" and
> > be sure it would mean the same thing for every compiler.

Sorry, this was a bad example off the top of my head.  The above actually
is well-defined because of the special behavior of void.  The only
downside to #define NULL ((void*)0) is that it can cause wrong
programs to work anyway on certain machines.  See
(http://www.spinellis.gr/news/8.html).

> 
> Yes you could.  The whole point of the (void *) type is to represent a
> generic pointer that can be assigned to any other pointer type _without_
> a cast.  That's why it was invented.  In C, the variable holds the type,
> not the value, so the (void *)0 automatically becomes an (int *) without
> a cast.

Maybe this is a (the?) source of confusion.  (void*)0 is not a
variable.  It is a special implementation-defined constant.

> 
> A pointer to a char is the same size as a pointer to an int and is the
> same size as a pointer to void.  

This is not generally true for typed null pointers.  Implementations
are free to use different representations for null pointers of
different types.  This has in fact been done.  Using the null-pointer
of the wrong type is not portable.  This is why David was right about
(void*) being better than (char*) on the sentinel.

-chris

> The only difference is that when you
> increment a char*, it clicks by one byte, and when you increment an
> int*, it clicks by sizeof(int) bytes.  Since int *i, above, is a pointer
> to int, it works as expected even though you initialize it with NULL.
> 
> -- 
> Karl Hegbloom <hegbloom at pdx.edu>
> 
> _______________________________________________
> gnucash-devel mailing list
> gnucash-devel at gnucash.org
> https://lists.gnucash.org/mailman/listinfo/gnucash-devel


More information about the gnucash-devel mailing list