Re: alignment
- From: "Vladimir S. Oka" <novine@xxxxxxxxxxxxxx>
- Date: Mon, 16 Jan 2006 16:02:02 +0000 (UTC)
Kies Lee wrote:
main(void) { union { char a[10]; int i; } u; int *p = (int*)&(u.a[1]); *p = 17; }
This would be expected to be quite slow, on current hardware platforms which can make it work. Not that you would notice if you did it only once. Besides, if it does "work," how can you check that it does the same thing on all the platforms of interest to you? I suppose, for many little-endian platforms, it would set u.a[1] to 17, and zero out u.a[2..4]. The effect on u.i would depend on what was there before. On a long-ago platform I learned on, it would (efficiently) zero out u.a[0..2], and set u.a[3] and u.i to 17, without affecting u.a[4]. What's the point, other than to check how your platform behaves in odd situations? You could do it more efficiently, portably, and readably, with a simple assignment and a memset().
I don't want to know how it work, but I want to know why it works.
If you learn how it works, you'll also find out why it works -- or not, as the case may be depending on the implementation/host system.
Because the address of the p(pointer) could be 3221217958(one possible value), it is not on the edge of the memory(cann't be divided by 4).
<OT> Apart from this being entirely irrelevant, I'm wondering why the fact that X is not divisible by 4 ensures it cannot be on the edge of memory? </OT>
I don't know why ?
Neither do I.
-- Kies
Cheers
Vladimir
-- My e-mail address is real, and I read it. .
- References:
- alignment
- From: Kies Lee
- Re: alignment
- From: Tim Prince
- Re: alignment
- From: Kies Lee
- alignment
- Prev by Date: Re: Portability / compatibility issues
- Next by Date: Re: fgets
- Previous by thread: Re: alignment
- Next by thread: Re: alignment
- Index(es):
Relevant Pages
|