>
> Hi !
>
> can someone explain to me whats happening here ?
>
> --simple.c--
> #include <linux/module.h>
> #include <linux/kernel.h>
>
> struct { short x; long y; short z; }bad_struct;
> struct { long y; short x; short z; }good_struct;
>
[SNIPPED...]
> ---------------------------------------------------------------
>
> I would expect both structs to be 8byte in size , or atleast the same size !
> but good_struct turns out to be 8bytes and bad_struct 12 .
>
> what am I doing wrong here ?
You are assuming something that is wrong. Many programmers use
a structure as a "template", assuming that what they write is
exactly what exists in memory. This is not how the 'C' standards
are written! The only thing guaranteed by the standard is that
the first structure member will exist as the same address as the
structure itself -- nothing else.
So that structures can be used as templates, many compilers including
gcc, have non-standard extensions that can be used to "pack" structure
members. __attribute__ ((packed)) will probably do what you want.
FYI, structures are designed to be accessed only by their member-names.
Therefore, the compiler is free to put members at any offset. In fact,
members, other than the first, don't even have to be in the order
written!
Cheers,
Dick Johnson
Penguin : Linux version 2.4.1 on an i686 machine (799.53 BogoMips).
"Memory is like gasoline. You use it up when you are running. Of
course you get it all back when you reboot..."; Actual explanation
obtained from the Micro$oft help desk.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/