Richard Tobin wrote:
> In article <_>,
> Eric Sosman <> wrote:
>
>>> M> You will be OK. char is always 1 byte. So casting an arbitrary
>>> M> pointer to a char *, adding an exact multiple of the size of
>>> M> the original type, and casting back is guaranteed to preserve
>>> M> alignment.
>
>>> I am not so sure about that; would you care to cite C&V, please, if
>>> you claim that it's guaranteed by the standard?
>
>> He's wrong: it's not guaranteed. Simple example:
>>
>> int target = 42;
>> int *ptr = &target + 1; /* "an arbitrary pointer" */
>> ptr = (int*)((char*)ptr + sizeof *ptr); /* U.B. */
>>
>> If the original pointer points at an actual object of its
>> type (so it's not "arbitrary"), the conversion is safe.
>
> That seems to be excessive pedantry. He said it "preserves
> alignment", not that it's legal. Presumably you would deny that
> adding 2 to an int preserves it odd/even parity, because you might
> choose INT_MAX.
Adding two to an "arbitrary" integer need not preserve
parity, although adding two to most integers does. For the
two problematic integers you get undefined behavior, after
which assertions about what is and isn't preserved -- or
pickled -- just evaporate.
Adding sizeof *ptr to an "arbitrary" value of ptr need
not preserve alignment, although adding it to a restricted
class of values certainly does. But once U.B. occurs, the
Standard abdicates and no longer supports arguments that
formerly relied on it. It is wrong to claim that alignment
preservation is "guaranteed" in light of U.B.; "guaranteed"
by whom or by what?
BTW, "excessive pedantry" is unnecessarily redundant.
--
Eric Sosman
lid