Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!sun-barr!lll-winken!xanth!cs.odu.edu!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.std.c Subject: Re: gcc and NULL function pointers. Message-ID: <1991Jun26.134355.29334@cs.odu.edu> Date: 26 Jun 91 13:43:55 GMT References: <16481@smoke.brl.mil> <1991Jun26.053508.3634@ringer.cs.utsa.edu> Sender: news@cs.odu.edu (News File Owner) Organization: Old Dominion University, Norfolk, VA Lines: 59 Nntp-Posting-Host: opium.cs.odu.edu Newsgroups: comp.std.c Subject: Re: gcc and NULL function pointers. Summary: Expires: References: <16481@smoke.brl.mil> <1991Jun26.053508.3634@ringer.cs.utsa.edu> Sender: Followup-To: Distribution: Organization: Old Dominion University, Norfolk, VA Keywords: In article <1991Jun26.053508.3634@ringer.cs.utsa.edu> djimenez@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@cs.odu.edu