[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Bug Report (offset+cast)

From: David A. Wheeler
Subject: Re: [Tinycc-devel] Bug Report (offset+cast)
Date: Fri, 11 May 2007 15:32:47 -0400 (EDT)

> > > Happens here too. However,
> > > short word = *(short*)(s+1);
> > > works as expected.
> > Confirmed, that one works correctly.

Dave Dodge:
> Well, the term "correctly" is a bit suspect.  You can't convert an
> arbitrary char* to a short* and dereference it without risking
> undefined behavior, because among other things there's no guarantee
> that the pointed-to location will be aligned properly as a short.

"Correctly" is probably not the right term; "as expected on an x86" is probably 
a good one, though. That construct is clearly NOT portable, because (as you 
noted) many systems REQUIRE alignment.  On the other, it's reasonable to expect 
the code to work on an x86, because alignment is REQUIRED to be merely an 
optimization (not a strict requirement) on an x86.

It'd be wiser to rewrite that code (e.g., as a memcpy to a short which you KNOW 
it's aligned), since no matter what it's non-portable.

--- David A. Wheeler

reply via email to

[Prev in Thread] Current Thread [Next in Thread]