struct
David Brown
david@westcontrol.com
Mon Oct 1 12:42:00 GMT 2018
On 30/09/18 19:44, sundaresh wrote:
> Why do struct's take up a lot more memory than the sum of the sizes of its
> members ? This increases by even a lot more the larger the struct gets . If
> the compiler is doing padding or aligning I would like to disable and not
> have that at all since it is unnecessarily wasteful of memory ? I
> encountered a similar problem when declaring floating point variables
> independently outside a struct. The problem becomes worse if these floating
> point variables are struct members.
>
A useful gcc flag here is "-Wpadded" - it will warn you if the compiler
has had to add padding in a struct to make the alignments fit. This
should show you where the wasted space is, and then you can try to
re-arrange your struct's fields to get a more efficient fit.
It is also possible to use the "packed" __attribute__ for structs which
skips the spaces, but that makes the struct incompatible with external
code and leads to inefficient unaligned accesses.
More information about the Gcc-help
mailing list