What is padding?explain in brief

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahendra23
    New Member
    • Jun 2007
    • 13

    What is padding?explain in brief

    Plz tell me about padding in C language
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    This is a very vague question, and could mean a lot of things.
    Please try to provide more information.... ..

    Comment

    • mahendra23
      New Member
      • Jun 2007
      • 13

      #3
      Originally posted by DeMan
      This is a very vague question, and could mean a lot of things.
      Please try to provide more information.... ..
      I mean padding in c lang

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Padding is used by the compiler to align your variables.

        Processors work with words. A word represents the size of the processor registers. It would be convenient if data in memory were aligned on word (32-bit) boundaries otherwise it would take two operations to get a variable into a register instead of one.

        Your compiler knows this and tries to optimize the layout of your variables.

        For example:
        [code=c]
        struct MyStruct
        {
        int a;
        char b;
        int c;
        };
        [/code]

        the compiler will add 3 pad bytes after b so that c is aligned on a 32-bit boundary. There may also be pad bytes added so that a is aligned on a 32-bit boundary.

        Normally you don't have to worry about this. However, because of this padding, you may find that:
        [code=c]
        sizeof(MyStruct ) != sizeof(a) + sizeof(b) + sizeof(c);
        [/code]

        Comment

        Working...