Structure Padding Tool

Feb 8, 2011 at 7:31pm
Hi everyone,

I am interested in a tool that parses through all the structs in my header files and determines where structure "padding" would be needed. For instance:

struct example {
char c1;
short s1;
char c2;
long l1;
char c3;
}

I would the above to be converted to:

c1
padding byte
s1 byte 1
s1 byte 2
c2
padding byte
padding byte
padding byte
l1 byte 1
l1 byte 2
l1 byte 3
l1 byte 4
c3
padding byte
padding byte
padding byte

I am not interested in re-arranging the fields, but rather add "pad" variables. Do you know if this tool exists? If not, do you have an idea on how this would be implemented?

Thank you.
Feb 8, 2011 at 7:43pm
For which platform? Which compiler? 32-bit or 64-bit? What about compiler options, pragmas or attributes?

http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html
http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html
http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options

Is this practical? Is it even possible to do in a portable manner?
Feb 8, 2011 at 9:25pm
Sounds nuts...
Feb 9, 2011 at 12:44am
Ahh I see, looks like it is not very portable. I am sort of looking for something similar to Perl's pstruct, which prints the offset and size of each variable in a structure, taking in consideration where it would be padded.

http://perldoc.perl.org/pstruct.html
Feb 9, 2011 at 6:54pm
You can vary the padding in C/C++. You can check the padding yourself by comparing the addresses of the fields and the size of the preceding item.
Topic archived. No new replies allowed.