Re: Unnecessary struct padding
Re: Unnecessary struct padding
- Subject: Re: Unnecessary struct padding
- From: Tom Seddon <email@hidden>
- Date: Tue, 23 Aug 2011 01:17:51 +0100
If you have an array of objects, naturally each element must be aligned. This effectively means that if (T *)X is aligned, (T *)X+1 has to be aligned too - since that is what array accesses boil down to. A struct may therefore need padding to make its size a multiple of its required alignment.
I think this is why the union of id+char[20] (28 bytes) has 4 bytes of padding - it ensures that if it were used in an array, each object would be appropriately aligned. (The alignment requirements are 8 bytes, I guess, since the first item is a 64-bit pointer.) Same goes for the struct enclosing it, and for the other struct too.
I would have expected that this requirement could be relaxed for unnamed struct types - certainly looks like it could be in this case. Perhaps there's some subtlety that prevents this. Or maybe it's just not implemented.
--Tom
On 22 Aug 2011, at 23:15, Benjamin Rister wrote:
> I’ve got a situation where I’ve got a huge number of things I want to fit in memory without chomping up all of the user’s RAM, so I’m trying to optimize the data structure down as much as possible. I’m running into issues where it appears the compiler (LLVM 2.0) is padding things that don’t seem to need to be padded for alignment.
>
> The struct looks like this. I’ve marked the places where it’s being padded unnecessarily:
>
> struct MyStruct {
> MyStructRef; // 8 bytes
>
> NSTimeInterval; // 8 bytes
>
> // Collectively, 8 bytes
> UInt8[3];
> UInt8[3];
> UInt8;
> UInt8;
>
> union {
> struct {
> struct MyStruct **; // 8 bytes
> union {
> id;
> char[20];
> // XXX: 4 bytes of unneeded padding here, as the UInt32 next is already 4-byte aligned
> };
> UInt32;
> // XXX: 4 bytes of unneeded padding here, since the UInt8 next has no alignment needs
> };
>
> struct {
> union {
> id;
> char[12];
> // XXX: 4 bytes of unneeded padding here, as the UInt8s next have no alignment needs
> };
> UInt8[20];
> }; // XXX: 4 bytes of unneeded padding here, since the UInt8 next has no alignment needs
> }
>
> UInt8;
>
> // I know there’s some slack here because malloc works in 8-byte intervals…this is a work in progress
> };
>
> Am I forgetting some rule about alignment or something that makes this padding legitimate or needed? My first choice would not be to disable the compiler’s padding altogether, because while it all lines up now, I don’t want to accidentally break it later without noticing—and if I’m forgetting a reason why this would be needed, just disabling it already could break something. Any suggestions?
>
> Thanks,
> br _______________________________________________
> 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
_______________________________________________
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