Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!nbires!hao!hplabs!tektronix!reed!kab From: kab@reed.UUCP (Kent Black) Newsgroups: net.lang.c Subject: Re: Failure of "long x(cp) char * cp; { return *((long *) cp); }" is a bug? Message-ID: <3783@reed.UUCP> Date: Thu, 3-Jul-86 14:24:49 EDT Article-I.D.: reed.3783 Posted: Thu Jul 3 14:24:49 1986 Date-Received: Sat, 5-Jul-86 04:59:32 EDT References: <6152@elsie.UUCP> Reply-To: kab@reed.UUCP (Kent Black) Organization: Reed College, Portland, Oregon Lines: 34 In article <6152@elsie.UUCP> ado@elsie.UUCP (Arthur David Olson) writes: >As I understand it, the code produced by some compilers from > > long x(cp) > char * cp; > { > return *((long *) cp); > } > >will cause core dumps if the value of cp isn't "appropriately aligned". > >Isn't this really a compiler bug? Doesn't the cast in the > return *((long *) cp); >give the compiler all the evidence it needs that it should, *on its own*, >produce code that's the equivalent of the function call? Oh, am I in over my head. . . If a (char *) can reference any `byte' of memory, and a (long *) can can only reference, as example, even number word addresses, then to have the compiler correctly cast a char * to a long * would mean it rearranged your data for you. In particular, an array of characters would have to be remade into an array of longs with the apropriate (high or low) byte holding the character. Is this all wrong? But do you *really want* to cast a char pointer to a long pointer just to dereference and return the value? The example seems to instead call for dereferencing the char * and casting the value as a long, e.g., return (long) *cp; I only respond because I'm not certain myself and would like some further discussion. Kent Black