Failure of "long x(cp) char * cp; { return *((long *) cp); }" is a bug?

John Woods, Software john at frog.UUCP
Wed Jul 2 02:57:23 AEST 1986


> 	long x(cp)
> 	char * cp;
> 	{return *((long *) cp); }
> will cause core dumps if the value of cp isn't "appropriately aligned".
> When dealing with such compilers, you get to do something like
> 	long x(cp)
> 	char * cp;
> 	{	long l;
> 		function_to_do_arbitrary_copying((char *) &l, cp, sizeof l)
> 		return l;
> 	}
> to avoid core dumps.
> Isn't this really a compiler bug?

No, I'd say not.  The compiler is doing just exactly what you said in the
first case.  What you said was something stupid, ergo it does something
stupid.  Lint will complain about the construction, declaring there to be
a "possible pointer alignment problem" (you bet!).

(by the way, an in-line copy would be faster than the function call (except
perhaps on machines where function calls take negative time :-) )).


--
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw%mit-ccc at MIT-XX.ARPA

"Imagine if every Thursday your shoes exploded if you tied them the usual way.
This happens to us all the time with computers, and nobody thinks of
complaining."
			Jeff Raskin, interviewed in Doctor Dobb's Journal



More information about the Comp.lang.c mailing list