Is it guaranteed that there is no padding between values ​​of the same type?

Consider this code:

// T is *any* type
struct str_T{
    T a, b;
};

I know that there (almost always) indents between objects with different alignments, because both elements are of type T. But this time there are no different alignments. Can this statement always pass?

static_assert(sizeof(str_T) == 2 * sizeof(T));
// i.e. padding-free
+6
source share
2 answers

No, this is not guaranteed. The compiler can always decide whether to use or not add extra bits between members of the structure. (If not overridden)

Quote from Project C11 , 6.7.2.1. Structure and Association Qualifiers

, , . , , ( -, , ) . ,

+6

, , .

C11 6.7.2.1 (p6):

, ,

.

+1

All Articles