```Reply to Andrei,

> BCS wrote:
>
>> would "align 1" work?
>>
> That could make things excruciatingly slow. What's really needed is
> (in first approximation) to sort the members in decreasing order of
> size. Odd-sized arrays of char foil this plan to some extent, which is
> where the fun begins.
>
> Andrei
>

??

http://en.wikipedia.org/wiki/Bin_packing_problem
```
```== Quote from BCS (ao@pathlink.com)'s article
> >
> ??
> http://en.wikipedia.org/wiki/Bin_packing_problem

Yes, but most structs are small enough that the asymptotic complexity really isn't
important.  For structs of maybe 4 or less elements, trying every possible
permutation in O(N!) seems perfectly reasonable to me.  For larger structs, using
heuristics might be necessary.
```
```dsimcha wrote:
> == Quote from BCS (ao@pathlink.com)'s article
>> ??
>> http://en.wikipedia.org/wiki/Bin_packing_problem
>
> Yes, but most structs are small enough that the asymptotic complexity really isn't
> important.  For structs of maybe 4 or less elements, trying every possible
> permutation in O(N!) seems perfectly reasonable to me.  For larger structs, using
> heuristics might be necessary.

You're only interested in the values of   x.sizeof&(x.alignof-1), and
x.alignof is only ever 16,8,4,2, or 1. So there are only 65(?) possible
elements.
I'm pretty sure it can be done in linear time, with the calculation
maybe even done in constant time.
```
```BCS wrote:
>
>> BCS wrote:
>>
>>> would "align 1" work?
>>>
>> That could make things excruciatingly slow. What's really needed is
>> (in first approximation) to sort the members in decreasing order of
>> size. Odd-sized arrays of char foil this plan to some extent, which is
>> where the fun begins.
>>
>> Andrei
>>
>
> ??
>
> http://en.wikipedia.org/wiki/Bin_packing_problem
>

It's not a bin packing problem because the restrictions are very
different. Clearly it's not a simple problem either! I wonder if it's
NP-hard, and I highly doubt it (in fact I'm almost sure it isn't)
because it can be easily decomposed into smaller subproblems.

Andrei
```