Re: Alignment in structs
Re: Alignment in structs
- Subject: Re: Alignment in structs
- From: John Daniel <email@hidden>
- Date: Sat, 15 Jul 2006 09:19:52 -0500
What do I need to set in Xcode to get the size of this struct:
typedef struct Blah { UInt32 a, b, c; SInt16 d; } Blah;
to be 14 instead of the 16 I'm getting currently? Thanks,
Larry
You could use a bitfield.
typedef struct Blah { UInt32 a : 32; UInt32 b : 32; UInt32 c : 32; SInt16 d : 16; } Blah;
If you try to struct to encapsulate a structure with a specific size, you are always going to have these kinds of issues. Your alignment can change if the compiler gets upgraded, you change your optimization settings, you change some other random setting, etc. It just isn't a reliable way to do it.
Even with a bitfield, the compiler can and will re-order your fields. There is no guarantee that d will not be the first field in the struct.
The best way to do this is something like this:
class Blah { private: char buf[14];
public: // Insert clever, bit-shifting getters and setters here. }
This way, you know exactly the size of your struct. You know exactly what your fields are, where they are, and how big they are. You also know that you have no alignment whatsoever. Your getters and setters do need to be clever.
If you don't want to use C++, you can easily do this with C. It just won't be as pretty.
John |
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden