Aligning on some odd address?

Brian Budge
Mon Apr 20 16:38:00 GMT 2009

Hi Philipp -

If you have to store the length inside of the inner structure, it makes the
structure larger, thereby bloating space.  If you have it once for an entire
array, you almost can't do worse (okay, bitfield-type packing aside).
Additionally, if your structures are 20 or 24 bytes, you are most likely
looking at 4 or (maybe) 8 byte alignment, unless one of the members of the
struct is 16 byte aligned.  If those are 32 byte aligned, you are wasting
space, and this would only make sense if you're doing something fancy with
DMA or something.

The thing to do is to take a look at what the inner structs look like
WITHOUT the piece that is checked for termination.  Then, based on the size
and alignment, we can figure out something fairly optimal.  Can you post a
couple of the structs that are kept in arrays?

On Mon, Apr 20, 2009 at 12:38 AM, Philipp Marek
<> wrote:
> Hello Brian!
> On Freitag, 17. April 2009, Brian Budge wrote:
>> I'm trying to see if I understand correctly:  There are 1260 arrays of
>> different types.  Each type is at least 16 bytes, but needn't
>> necessarily be 16 byte aligned.  If each array is terminated by an
>> empty (zeroed) structure, that would be 1260*16 bytes of wasted
>> memory.  Now if the arrays are large this is probably a very minor
>> amount of waste, but if the arrays are small, this could be a lot of
>> waste.  You were using an unsigned char for your length variable,
>> which makes me think that the lengths could be quite small.
> The biggest array is ~500 elements long, so I'd need 9 bit - a short.
>> You
>> almost certainly won't gain anything by using a 1 byte length, as your
>> inner structs are very unlikely to be 1 byte aligned.  Most likely
>> they will be at least 4 byte aligned, so you might as well use a 4
>> byte integer.  An additional benefit is that if you are iterating over
>> the structure, it should be faster if a memcmp isn't needed, and a
>> simple counter can just be checked.
> It's not a memcmp, just a compare whether siome member of the structure == 0.
>> If you put the length in front of the memory, how much space is wasted
>> will depend on the size of your length (you were using char), and the
>> alignment of the structs in the array.  Although I'm not sure of the
>> guarantee (I haven't read the standard), I believe every sane compiler
>> will layout the memory of the structs to reflect the alignment of it's
>> members.
>> So in other words, how much space you waste depends on the alignment
>> of the inner structs, and the outer struct should take on the
>> alignment of the maximal alignment requirement of it's members.
>> You stand to save a small amount of space, but also to potentially
>> make the code more readable and more efficient by having the length
>> instead of the zeroed-struct terminator.
> Well, I could try to get the "terminating" structure to have the flag at the
> first position, so I could omit the rest of the structure - which would amount
> to the same savings.
> Well, some of these structures have sizes of 20 or 24 bytes; I'm not sure
> whether they are aligned on 20, 24 or even 32 bytes in memory - I'm still
> thinking various ways to save some memory.
> Thank you for your answers!
> Regards,
> Phil

More information about the Gcc-help mailing list