Structure Padding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arunraj2002in
    New Member
    • Jun 2007
    • 22

    Structure Padding

    how to find padding done by compiler for a structure?
    For ex:
    struct
    {
    int i1;
    char c1;
    int i2;
    } pad;

    here the sizeof(pad) will be 12. Now i want to know is there any method/program to find out compiler output (*)for padding?

    (*) struct
    {
    int i1;
    char c1;
    paddin;
    paddin;
    paddin;
    int i2;
    } pad;
  • OuaisBla
    New Member
    • Jun 2007
    • 18

    #2
    Padding (or packing) is a compiler option.

    What compiler are you using? It can also be controlled using #pragma directive.

    Most compilers have default value of 8 (field are aligned on 8 bytes boundaries)

    So:

    struct S
    {
    double d1;
    char c;
    double d2;
    };

    will have sizeof(S) == 24;

    Look for:

    #pragma pack

    Or maybe something like:

    __declspec(alig n(32)) struct Str1{
    int a, b, c, d, e;
    };

    At http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

    And also __alignof operator exists but is it Microsoft specific (probably VS2005 or more)

    Comment

    • arunraj2002in
      New Member
      • Jun 2007
      • 22

      #3
      Hi,
      Actually i don't want to modify structure, i just want to know that is their any to find compiler padded structure as output.
      Ex: struct{ int a; char b; int c;} .. now after padding the structure will be
      struct{ int a; char b;pad1; pad2; int c;}.. i actually want this as an output.


      Originally posted by OuaisBla
      Padding (or packing) is a compiler option.

      What compiler are you using? It can also be controlled using #pragma directive.

      Most compilers have default value of 8 (field are aligned on 8 bytes boundaries)

      So:

      struct S
      {
      double d1;
      char c;
      double d2;
      };

      will have sizeof(S) == 24;

      Look for:

      #pragma pack

      Or maybe something like:

      __declspec(alig n(32)) struct Str1{
      int a, b, c, d, e;
      };

      At http://msdn2.microsoft.com/en-us/lib...65(VS.80).aspx

      And also __alignof operator exists but is it Microsoft specific (probably VS2005 or more)

      Comment

      Working...