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

Kent Black kab at reed.UUCP
Fri Jul 4 04:24:49 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".
>
>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



More information about the Comp.lang.c mailing list