Author: Gareth McCaughan
Date: 14:24:41 05/11/03
Go up one level in this thread
Robert Hyatt wrote:
>>> left-to-right means "ascending address order"...
>>
>> If that's what you meant by it, then that bit was correct.
>> But "from will be the MSbyte, piece will be the LSbyte"
>> is guaranteed only if you know the architecture the
>> thing is being compiled for. Ascending address order
>> needn't mean most significant byte first.
>>
>
> In the context given, my answer is correct. He put four chars into a
> struct. The first char will definitely be the "MSB" which is the left-most
> byte or the byte with the lowest memory address. The second char will follow
> sequentially...
>
>There can be no exception to that.
"Most significant" doesn't mean "lowest address". It means "represents
the largest power of 256 when viewed as an integer". And, as I said,
on ia32 the most significant byte has the *highest* address within
a word.
$ cat endian.c
#include <stdio.h>
int main(void) {
union { struct { char a,b,c,d; }; int i; } x;
x.i = 0x01020304;
printf("%d %d\n", x.a, x.b);
return 0;
}
$ gcc endian.c
$ ./a.out
4 3
The first char in the struct gets the lowest address, and
the *least* significant byte of the integer.
>> In fact, on ia32 systems it's exactly the other way around,
>> so if Zach's code is going to run on a PC then "from" will
>> be the least significant byte and "piece" the most.
>
> Only if you load the entire thing at one "gulp" from something like
> mov eax, struct_addr.
If you don't do that, then the terms "least significant" and
"most significant" don't mean anything.
>>>> There may be padding anywhere within the structure, including
>>>> between those single-byte fields.
>>
>> Not that this is likely to be a problem in practice, in this
>> particular case.
>
> I'm not aware of padding between chars. It would break _everything_.
I'm fairly sure it's legal, even so. But it doesn't happen in practice.
--
g
This page took 0.01 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.