endian dilemma

Jerry Feldman gaf at blu.org
Fri Sep 6 14:06:04 EDT 2002


On 6 Sep 2002 at 13:51, Derek D. Martin wrote:
> Am I counting wrong?
> 
> > struct {
> > 	char a;   /* 1 byte */
> > 	long b;   /* 8 bytes on a 64 bit system, 4 bytes on 32*/
> > };
I'll restate. I mixed up my 64 bit and 32 bit examples.
The size of the struct on a 32 bit CISC system might be 5 bytes, but on a 
32 bit RISC would be 8 bytes long, because field b must be aligned on a 32 
bit boundary.

The bottom line is that alignments vary not only by processor (all RISC 
processors require natural alignment, but CISC processors do not). Some 
compilers may align differently on the same system. Calling standards also 
come into play. The size of a structure may be longer than the sum of its 
element sizes. 
Also, assume I am on a RISC processor (eg. UltraSparc):
#pragma pack on /* specific syntax is probably different */
struct {
 	char a;   /* 1 byte */
 	long b;   /* 8 bytes on a 64 bit system, 4 bytes on 32*/
} foo;
#pragma pack off
long c = foo.b; /* This will cause an exception */
Now assume Tru64 Unix or Linux on Alpha (64 bit).
long c = foo.b; /* This will NOT cause an exception OS will perform a fixup 
*/

-- 
Jerry Feldman <gaf at blu.org>
Associate Director
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9




More information about the Discuss mailing list