kommen die 2 zusätzlichen Bytes? Ich habe gehört, dass das mit
Optimierungsgründen zusammenhängt. Wenn ja, was bedeutet das
genau?
Ja in gewissermassen, damit der zugriff auf die einzelnen member von einer structur möglichst schnell geht, erzeugt der Compiler sogenannte padding (also zwischenräume) . Das garantiert auch kompailibilät.
Mann kann es abschalten das es ohne lücken gemacht wird, ist allerdings langsam und nciht kompatibel mit so einigem
Hab nur was in englisch gefunden das das erklärt.
To produce faster code, GCC pads struct members so that each one can be accessed without delays; this sometimes produces struct size which is larger than the sum of the sizes of its members. If you need to minimize this padding (e.g., if your program uses large arrays of such structs, where padding will waste a lot of memory), lay out your structures so that the longer members are before the shorter ones. For example, let's say that you have a struct defined thus:
struct my\_struct {
char name[7];
unsigned long offset;
double quality;
};
To make such a struct use the least number of bytes, rearrange the members, like this40:
struct my\_struct {
double quality;
unsigned long offset;
char name[7];
};
If the layout of the structure cannot be changed (e.g., when it must match some external specification, like a block of data returned by a system call), you can use the \_\_attribute\_\_((packed)) extension of GCC (see GNU C/C++ Manual.) to prevent GCC from padding the structure members; this will make accesses to some of the members significantly slower.
Beginning with version 2.7.0, GCC has a command-line option -fpack-struct which causes GCC to pack all members of all structs together without any holes, just as if you used \_\_attribute\_\_((packed)) on every struct declaration in the source file you compile with that switch. If you use this switch, be sure that source files which you compile with it don't use any of the structures defined by library functions, or you will get some members garbled (because the library functions weren't compiled with that switch). Also, GCC 2.95.1 and 2.95.2 had bugs in their support of -fpack-struct (the bug is corrected in v2.96 and later).