Re: pointer conversion
- From: richard@xxxxxxxxxxxxxxx (Richard Tobin)
- Date: 31 Aug 2007 19:48:39 GMT
In article <_eidnezKNPZ67EXbnZ2dnUVZ_vShnZ2d@xxxxxxxxxxx>,
Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxxx> 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.
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
.
- Follow-Ups:
- Re: pointer conversion
- From: Eric Sosman
- Re: pointer conversion
- From: Malcolm McLean
- Re: pointer conversion
- References:
- pointer conversion
- From: junky_fellow@xxxxxxxxxxx
- Re: pointer conversion
- From: Malcolm McLean
- Re: pointer conversion
- From: Charlton Wilbur
- Re: pointer conversion
- From: Eric Sosman
- pointer conversion
- Prev by Date: Re: How to force fscanf to find only data on a single input line?
- Next by Date: Re: size_t problems
- Previous by thread: Re: pointer conversion
- Next by thread: Re: pointer conversion
- Index(es):
Relevant Pages
|