gcc and NULL function pointers.

Lloyd Kremer kremer at cs.odu.edu
Wed Jun 26 23:43:55 AEST 1991


Newsgroups: comp.std.c
Subject: Re: gcc and NULL function pointers.
Summary: 
Expires: 
References: <16481 at smoke.brl.mil> <XZ4C6T6 at xds13.ferranti.com> <1991Jun26.053508.3634 at ringer.cs.utsa.edu>
Sender: 
Followup-To: 
Distribution: 
Organization: Old Dominion University, Norfolk, VA
Keywords: 

In article <1991Jun26.053508.3634 at ringer.cs.utsa.edu> djimenez at ringer.cs.utsa.edu (Daniel Jimenez) writes:
>I thought 0 in a context where a pointer is expected (e.g., int *p; p = 0;)
>wasn't the integer 0, rather whatever that machine's representation of
>a null pointer is.

Yes.  But problems most often occur when the compiler does not recognize that
a pointer interpretation is appropriate such as when passing the pointer 0 as
a function argument with no cast and no prototype in scope.

>Anyway, if you have a machine where pointers to different types are
>of different sizes, then you have much bigger problems than what to
>#define NULL as.

Yes, the ANSI committee had to grapple with these problems, and solved them.

>Like, let's say your machine has one size for 
>character pointers and another for integer pointers.

Yes, there are some like that.  All that I've heard of have the char* larger
than the int*, since the char* contains both a word address and a byte offset.

>What if you wanted to allocate space for an integer like this:
>
>int *p;
>...
>/* decision made to allocate integer */
>p = (int *) malloc (sizeof (int));
>
>malloc would return a value of one size which would be a valid character
>pointer, but the wrong size for an integer pointer.

A conformant malloc would return a void* that had been specially selected
to comply with the most stringent alignment requirements of the architecture,
so as to be safely castable to any other pointer type.  In the case of
"char* larger than int*" all of the bits that the char* has but that the int*
doesn't have would be zero, so that the pointer cast (i.e. truncation) would
be harmless and reversible.

>For what it's worth, here's my opinion on NULL:
>We should all contribute to a fund to help build a time machine so
>someone can go back in time and tell K&R to include something like
>Pascal's nil in C. :-)

OK, I'll hold the money until funding is complete.  :-)

					Lloyd Kremer
					Hilton Systems, Inc.
					kremer at cs.odu.edu



More information about the Comp.std.c mailing list