Hi,
I have a question to that struct:
I thought sizeof(Buchung) would be 124 Bytes 8 + 11 + 4 + 101 but it is 128. Why?Code:typedef struct b
{
double amount;
char date[11];
struct buchung *next;
char description[101];
} B;
Thanks,
keeper
Printable View
Hi,
I have a question to that struct:
I thought sizeof(Buchung) would be 124 Bytes 8 + 11 + 4 + 101 but it is 128. Why?Code:typedef struct b
{
double amount;
char date[11];
struct buchung *next;
char description[101];
} B;
Thanks,
keeper
The compiler may add extra bytes between members in a structure, so that some member (probably the *next pointer here) will be aligned on a certain boundary in memory. Having things aligned on 4,8,etc byte boundaries can boost performance, or is sometimes just required. (The same rules apply to the structure as a whole, so bytes may be added to the end) It's your compiler taking care of the detail work so you don't have to.
The layout is most likely 8 + 11 + 1 byte padding + 4 + 101 + 3 bytes padding. You can use the "offsetof" macro to report the offsets. Think of a struct as an array element itself. If you were to have an array of b structs, they would be required to have the same alignment for every element and have no gaps due to alignment in between elements. Adding the padding where I indicated would meet those requirements, and it matches your results as well.
Todd