Compiler padded structure as output.

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

    Compiler padded structure as output.

    Is their any way 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. Is it possible & how?
  • Silent1Mezzo
    New Member
    • Feb 2007
    • 208

    #2
    Originally posted by arunraj2002in
    Is their any way 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. Is it possible & how?
    I'm pretty sure this isn't possible. You could find how much the compiler added in padding by doing sizeof(struct name);

    You would just take that number and subtract the sizeof every individual item inside the struct.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Create an object of your struct and take the address of each member.

      If you have a char, the address of the next member subtracted from the address of the char member will give the pad on the char.

      [code=c]
      struct Test
      {
      int a;
      char b;
      int c;
      };
      int main()
      {
      Test obj;
      int b = reinterpret_cas t<int>(&obj.b) ;
      int c = reinterpret_cas t<int>(&obj.c) ;
      cout << c - b << endl;
      }
      [/code]

      Visual Studio.NET shows c -b as 4. That is, the char occupies 4 bytes. Hence, there are 3 pad bytes after the char.

      Comment

      Working...