• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Alignment in structs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alignment in structs


  • Subject: Re: Alignment in structs
  • From: John Daniel <email@hidden>
  • Date: Sat, 15 Jul 2006 09:19:52 -0500

On Jul 14, 2006, at 6:04 PM, email@hidden wrote:
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

  • Prev by Date: Re: Compile for Universal results in Intel only build
  • Next by Date: Re: warning: enumeral mismatch
  • Previous by thread: Re: Alignment in structs
  • Next by thread: warning: enumeral mismatch
  • Index(es):
    • Date
    • Thread