I have a struct such as
typedef struct
{
int a; // Let's say this ends up being 4 bytes
int b; // 4 bytes
char text[10]; // 10 bytes
} blah_t;
static blah_t myvar;
- Suppose the sum of fields’ sizes is 18 bytes in
blah_t
, butsizeof(blah_t)
is 20 due to padding. - The
myvar
is static, therefore it will be zero initialized.
Question:
- Are padding bytes 19 and 20 guaranteed to be 0 for a static variable?
If not, I would need to do
memset(&myvar, 0, sizeof(blah_t))
for anymemcmp
of the struct to be valid – even for a static variable. - What about
calloc(1, sizeof(blah_t))
? Are bytes 19 and 20 guaranteed to be zero? I believe this is the case.