Hi Paul, > > 1) What is the alignment problem if the array element type is 'char'? > > Suppose we have a platform where the alignment of each basic type is > equal to its size, where sizeof (int) == 4 and sizeof (char) == 1, and > where we have 'struct s { int n; char d[]; };' and suppose we want to > allocate a 'struct s' with a 3-element flexible array member 'd'. Then > 'offsetof (struct s, d) + 3 * sizeof (char)' is 7, and when we call > 'malloc (7)' malloc is entitled to assume that memory is being allocated > for an array of 7 one-byte objects, so it can return an address that is > not a multiple of 4, which means that the pointer that malloc returns is > invalid for 'struct s *'. To fix the problem, we can use 'malloc > (FLEXSIZEOF (struct s, d, 3))' instead, as FLEXSIZEOF yields 8 on this > platform, and malloc (8) must return an address that is a multiple of 4. > > > don't we need to apply a 'ceil'-like alignment to the malloc result? > No, because if malloc is given an argument like 8 that is a multiple of > sizeof (int), it must return a pointer suitable for 'int *'.
Oh, I see. Thanks. I was assuming that malloc (N) always returns a multiple of max_align_t. > Perhaps I should update the comment to make this all clearer, though it > is hard to be clear and accurate and terse in this area. You found the right words now :) - I would expect to see such explanations in the .h file, not in the .m4 file. Bruno