c - why 'Sizeof' value differs from the number of bytes before the flexible-length member of a struct? -
typedef struct { /*has 15 pointers*/ // ==> 15 x 4 = 60 bytes ; uint16_t; // ==> 2 bytes uint16_t array[]; } dummy_struct;
case-a) sizeof(dummy_struct)
returns 64 bytes
case-b) while if try print,
((int) &(((dummy_struct *)(0))->array[0]))
prints 62 bytes.
this prints 62 bytes well: ((int) &(((dummy_struct *)(0))->array))
i don't understand why there change in value? shouldn't sizeof()
return 62 well?
if there's padding of 2 bytes, shouldn't happen before flexible-length member in struct? if that's case, shouldn't case-b print 64 instead of 62?
edit:
typedef struct { uint32_t temp; uint8_t array[][6]; } dummy2; printf("%d %d\n", sizeof(dummy2), offsetof(dummy2, array)); // prints 4 4 printf("%d \n",((int) &(((dummy2 *)(0))->array[0]))); // prints 4
how come same effect not happening previous example? padding doesn't seem happen here. so, possible reason previous example padding happening after flexible-size member?
the sizeof
returns size of entire structure, while other 2 expressions return offset of array
member, happens 2 bytes before end of struct
. can same result offsetof
, this:
offsetof(dummy_struct, array) // 62
Comments
Post a Comment