Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!linus!philabs!cmcl2!seismo!brl-tgr!gwyn From: gwyn@brl-tgr.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: Re: Need some examples Message-ID: <317@brl-tgr.ARPA> Date: Tue, 30-Jul-85 17:02:26 EDT Article-I.D.: brl-tgr.317 Posted: Tue Jul 30 17:02:26 1985 Date-Received: Thu, 1-Aug-85 05:10:47 EDT References: <299@brl-tgr.ARPA> Organization: Ballistic Research Lab Lines: 48 > There are a variety of funny little problems with using the 0 > pointer in C. There is no "the" 0 pointer. There are an infinite variety of null pointers, and only in certain contexts (e.g., comparison against 0 integer constant) is one permitted to pretend otherwise. This subject is discussed in this newsgroup every few months. > calling f(0) is dangerous. But my friend contends that calling > f() with a zero char pointer, e.g. f((char *)0), is safe, because > (char *) is the largest pointer, and that he doesn't know of a C > implementation that doesn't pass all pointers as the largest > possible pointer (ala the way floats always get passed as doubles). "Ignorance is no excuse." > I suspect the argument is wrong (and dangerous), but don't have > examples of machines on which this trick fails. Anybody got > ammunition out there to help my side out? Any "trick" whose working is not guaranteed by the language definition should not be relied upon! Your organization seems to have a real problem with people giving bogus advice. Why in the world would anyone WANT to formalize such a "trick"? You should not need an actual implementation example. I will give a hypothetic example, though. Perhaps some real implementation is like this, perhaps not; the fact that it is permitted should rule out using the "trick", which will not work in this case. There are also other possible ways that the "trick" could fail, including the use of tagged architectures, non-zero bit patterns for null pointers, and so on. EXAMPLE: Consider a 16-bit word-addressed architecture (e.g., CDC 1700, H-316, perhaps DG Nova). It is possible to represent any (int *) in a single word, but it takes 2 words to represent a (char *). Consider the function void foo( int a, int *b, int c ); Suppose that for economy's sake (very important on 16-bit machines) the stack size of a function parameter is the minimum number of words needed to hold it (1 word for each of the 3 parameters in this example). Now, suppose one invokes the function as foo( 1, (char *)0, 3 ); Because of the parameter layout, either the first or the third parameter will not be transmitted correctly (it will have part of the (char *) in its place on the stack frame). QED.