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