We know that there is padding in some structures in C. Please consider the following 2:
struct node1 {
int a;
int b;
char c;
};
struct node2 {
int a;
char c;
int b;
};
Assuming sizeof(int) = alignof(int) = 4 bytes:
sizeof(node1) = sizeof(node2) = 12, due to padding.
What is the performance difference between the two? (if any, w.r.t. the compiler or the architecture of the system, especially with GCC)
These are bad examples - in this case it doesn't matter, since the amount of padding will be the same in either case. There will not be any performance differences.
The compiler will always strive to fill up trailing padding at the end of a
structor otherwise using arrays of structs wouldn't be feasible, since the first member should always be aligned. If not for trailing padding in some itemstruct_array[0], then the first member instruct_array[1]would end up misaligned.The order would matter if we were to do this though:
Assuming 4 byte
intand 4 byte alignment, thenboccupies 1+3 bytes here, anddan additional 1+3 bytes. This could have been written better if the twocharmembers were placed adjacently, in which case the total amount of padding would just have been 2 bytes.