N.B: This is similar, but not quite the same as Effects of __attribute__((packed)) on nested array of structures?
I am defining a struct type that contains several nested structs. One of the members is an array of packed structs which has me slightly confused on the order it should be nested in, vis-a-vis the norm of having the larger members first.
if the member is an array of structs that are 8 bytes each, with length 4, is the member a total of 32 bytes that is treated as a singular entity for packing and alignment, making another single struct member of, say 18 bytes, actually smaller?
e.g
typedef struct __attribute__((__packed__)) settingsProfile {
/* For packing and alignment, is this member 32 bytes or 4 'chunks'(?) of 8 bytes?*/
struct __attribute__((__packed__)) load {
int32_t slewRate;
int32_t current;
} load[4];
/* 18 bytes, so if the above *isn't* 32 it should be below this */
struct __attribute__((__packed__)) ac {
int32_t slewRate;
int32_t voltage;
int32_t iLimit;
int32_t ovp;
bool dcMode;
};
struct __attribute__((__packed__)) xfmr { // 4 bytes
int32_t ocp;
} xfmr;
uint16_t extOtp[2]; // 4 bytes
} settingsProfile_t;
Thanks!
This struct
is a type not a variable:
...
/* 18 bytes, so if the above *isn't* 32 it should be below this */
struct __attribute__((__packed__)) ac {
int32_t slewRate;
int32_t voltage;
int32_t iLimit;
int32_t ovp;
bool dcMode;
};
...
so it's size will not be included in the sizeof(settingsProfile_t)
.
You may looking for:
typedef struct __attribute__((__packed__)) settingsProfile {
uint16_t extOtp[2]; // 4 bytes
struct __attribute__((__packed__)) xfmr { // 4 bytes
int32_t ocp;
} xfmr;
// total 17
struct __attribute__((__packed__)) ac {
int32_t slewRate; // 4
int32_t voltage; // 4
int32_t iLimit; // 4
int32_t ovp; // 4
bool dcMode; // 1
} foo; // << ***here***
// total 32
struct __attribute__((__packed__)) load {
int32_t slewRate; // 4
int32_t current; // 4
} load[4];
} settingsProfile_t;
In my compiler the total sizeof(settingsProfile_t)
. is 57
, as the numbers explains.