I am having a struct with three fields defined as follows:
struct tmp {
char *ptr;
unsigned int data1;
unsigned int data2;
};
After compiled with GCC on a 64-bit system using Intel sandybridge processor, the sizeof(tmp) returns 24.
To my understanding, the compiler pads 4 bytes to both "unsigned int" fields. However, could it be better if there is no padding and the resulting structure is of size 16?
Imagine if there is an array of such structs, by forcing the struct to have a size of 16 would make sure there is no single struct within the array being split over cache lines, since the cache line size is 64 bytes for Intel SandyBridge processors. Therefore reducing the chance to have two memory accesses to acquire such a struct when looping through the array.