While debugging a problem, the following issue came up. (Please ignore minor code errors; the code is just for illustration.)
The following struct is defined:
<
As Christoph said, there are no guarantees regarding the padding. Your best bet is to not use memcmp
to compare two structs. It works at the wrong abstraction level. memcmp
works byte-wise at the representation, while you need to compare the values of the members.
Better use a separate compare function that takes two structs and compares each member separately. Something like this:
int box_isequal (box_t bm, box_t bn)
{
return (bm.x == bn.x) && (bm.y == bn.y);
}
For your bonus, the three objects are separate objects, they are not part of the same array and pointer arithmetic between them is not allowed. As function local variables, they are usually allocated on the stack, and because they are separate the compiler can align them in any way that is best, e.g. for performance.