c++ - Counting overhead due to packing in C (gcc/g++) -



c++ - Counting overhead due to packing in C (gcc/g++) -

i'd count/sum overhead in object file due packing (and, ideally, have gcc minimize me).

for example, consider next construction (32 bit x86, gcc):

struct { uint8_t a_char; uint32_t an_integer uint8_t another_letter; };

while actual info takes 6 bytes, construction takes 12 bytes in memory because both chars followed 3 padding bytes. reordering construction follows:

struct b { uint32_t an_integer uint8_t a_char; uint8_t another_letter; };

the construction have sizeof(struct b) == 8 (still 4 bytes of overhead).

(1) ideally, i'd gcc rearrange struct a struct b , save me space, version (4.2) doesn't seem optimization level.

(2) alternatively, given struct a, i'd (automatically) either number 6 (total amount of overhead) or 4 (minimal amount of overhead, if members ordered "ideally"). purpose of determine whether or not manually reordering structures worth time (likely not).

is there way gcc (1), , there tool perform (2)? closest thing can think of (1) #pragma pack(1), (i'm guessing) have serious performance implications making most/all memory accesses unaligned. (2), i'm thinking perl script parsing debugging symbols might able this, i'm not familiar plenty dwarf know sure.

for #1, reason it's not done both c , c++ standards prohibit construction fellow member reordering.

yes, struct packing cut down performance. and, mentioned in comment, in cases on non-x86 architectures can sigbus if seek operate on member.

for #2, yes perl script might able it. instead of parsing dwarf info, seek scanning source code struct definitions, , maybe generate little test programs check sizeof() of structs , members , on.

c++ c gcc alignment packing

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -