Hi.
I have a problem with structure alignment.
This is on x64, so word length is eight bytes.
I have three structures;
Code:
#pragma pack( push, 16 )
struct lfds700_freelist_element
{
struct lfds700_freelist_element
*next[2];
void
*user_data;
unsigned long long int
tail_padding[2];
};
struct lfds700_stack_element
{
struct lfds700_stack_element
*next[2];
void
*user_data;
unsigned long long int
tail_padding[2];
};
struct test_element
{
struct lfds700_freelist_element
fe;
struct lfds700_stack_element
se;
unsigned int
datum;
};
#pragma pack( pop, 16 )
I use an aligned malloc to allocate an array of struct test_element, aligned on a 16 byte boundary.
When I run the programme and examine the first element of that array, I see that in the struct lfds700_freelist_element, next is 16 byte aligned, user_data is 16 byte aligned (it follows with no padding), but tail_padding then ALSO follows with no padding - e.g. is 8 byte aligned. This then means the next member of the following struct lfds700_stack_element is 8 byte aligned and I crash, due to memory mis-alignment when performing cmpxchg16b.
If I have 16 byte packing, how can ANY member of a struct be on an 8 byte boundary?
Now, I expected structs to be tail padded so that they could be array allocated and I was crashing, so I added the tail_padding members; I think in fact these are unnecessary and tail padding porbably does occur - but I have left them in, because they should guarantee the extension of the struct to the 16 byte boundary and they don't do any harm with regard to the question itself.
So, it seems to me the freelist user_data member is not being padded by eight bytes when it should be. What am I missing? is there some behaviour due to the use of the structures inside a parent structure?
This is with the current Microsoft command line SDK.
Debugger screenshot showing memory locations is here - http://www.liblfds.org/debugger.png
(That image is also attached but appears to have been resized and it fairly illegible; it is not clear how now to remove that attachment :-/