lang2
lang2

Reputation: 11986

gcc optimisation effect on structure size

Say I have a large struct, which includes other struct, etc. Would gcc -os or any other gcc optimisation switch change the way it's stored in memory? I.e. would it pack the structure so as to squeeze out some extra space?

thanks,

Upvotes: 3

Views: 1357

Answers (3)

sehe
sehe

Reputation: 393934

In fact, I can see how aligning structs (by padding them) could lead to shorter code (no cross-boundary word addressing -> fewer load/stores)

-Os optimizes for binary size (i.e. most commonly referred to as code size) not memory compression

Upvotes: 0

Jason
Jason

Reputation: 32538

No, in order change the native platform alignment for a structure in gcc you would have to explicitly use the __attribute__((packed)) or __attribute__((align X)) compiler directives, or other gcc command-line switches that specifically direct the compiler to change the native-platform alignment for data-structures.

Also, packing a structure with mixed data-types so that all the data-members may not be aligned on a proper word-boundary in memory actually will be slower for accessing a data-member at runtime, not faster. This is because the compiler will have to unpack the structure back to the native alignment for the platform before accessing the data-member.

Upvotes: 4

Paul R
Paul R

Reputation: 213200

No, this should not happen - so long as you have the same alignment and packing options for all your code modules then they should work correctly together even if compiled with different optimisation levels,

Upvotes: 3

Related Questions