This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re:sizeof and allignment on 32bit target
- From: Eljay Love-Jensen <eljay at adobe dot com>
- To: "Eric de Jong" <list_ericdejong_10 at gmx dot net>, Roman Kellner <maillists at gmx dot ch>, gcc-help at gcc dot gnu dot org
- Date: Fri, 25 Jul 2003 05:43:20 -0500
- Subject: Re:sizeof and allignment on 32bit target
#define _ALIGN1_ __attribute__ ((aligned (1)))
#define _ALIGN2_ __attribute__ ((aligned (2)))
#define _ALIGN4_ __attribute__ ((aligned (4)))
#define _PACKED_ __attribute__ ((packed))
Caution: note that _ALIGN1_, _ALIGN2_, _ALIGN4_ and _PACKED_ are all reserved identifiers.
All identifiers with two adjacent underscores anywhere in the identifier is reserved for compiler use.
All identifiers with a leading underscore followed by a capital letter is reserved for compiler use.
2nd caution: packing and aligning/realigning/misaligning may cause SEGBUS errors and porting issues. Strongly NOT recommended.
Often "packing" is desired for writing out a structures "as-is", binary, to disk or over an IP connection. I highly recommend writing a utility routine to do that for you. Example....
struct TestStruct {
struct Value {
unsigned long Value1;
unsigned long Value2;
unsigned short Value3;
} Value;
unsigned long Value4;
void write(std::ostream& binaryStream);
void read(std::istream& binaryStream);
} Test;
Note: the << and >> streaming operators really should be for textual (human-readable) formats, not binary formats. The write/read methods are the ones ( by convention) for the serialization/marshalling of canonical binary formats.
For my own use, I would do something like:
struct TestStruct {
struct Value {
unsigned long Value1;
unsigned long Value2;
unsigned short Value3;
} Value;
unsigned long Value4;
void write(std::ostream& binaryStream);
void read(std::istream& binaryStream);
private:
typedef char byte; // Or "typedef char octet" if you prefer.
struct TestStructRec { // I/O, binary layout.
byte Value_Value1[4]; // MSB
byte Value_Value2[4]; // MSB
byte Value_Value3[2]; // MSB
byte Value4[4]; // MSB
};
} Test;
And at some point, I'd change 'struct TestStruct' to 'class TestStruct', because it's beginning to look less-and-less like a passive data structure, and more like an object (or actor) that knows about itself and can perform a set of operations (such as write and read).
HTH,
--Eljay