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

Chris Torek chris at umcp-cs.UUCP
Mon Jun 30 00:15:15 AEST 1986


In article <6152 at elsie.UUCP> ado at 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".

>...  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?

[\S = section symbol]

No.  K&R, Appendix A, \S 14.4, p.~210:

  14.4  Explicit pointer conversions
  ...
    A pointer to one type may be converted to a pointer to another
  type.  The resulting pointer may cause addressing exceptions
  upon use if the subject pointer does not refer to an object
  suitably aligned in storage. ...

While you are verifying this, take a look at \S 16.  I think it is
amusing that as long ago as 1978, K&R identified most of the major
portability problems that many people are now seeing for the first
time.  (The big one they left out of \S 16 is null-pointer derefer-
encing.)  You were warned, eight years ago!  :-)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list