Structure padding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wildc
    New Member
    • Mar 2008
    • 4

    Structure padding

    Hello friends,
    I have gone through various sites and understood what is structure padding, But no body tells it in the context of a microcontroller .
    In my view, for example in a 32 bit machine following structure will be padded like this
    struct
    {
    char a;
    int b;
    short int c;
    char d;
    } ;

    [IMG][/IMG]
    My question are

    1)What happens if this structure is defined on 8 bit and 16 bit machines

    2)How padding helps compiler for efficient programming even if it takes more space.

    3)Finally why only structures are padded. Why not normal variables
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by wildc
    1)What happens if this structure is defined on 8 bit and 16 bit machines

    2)How padding helps compiler for efficient programming even if it takes more space.

    3)Finally why only structures are padded. Why not normal variables
    1) it depends on the processor.

    2) it doesn't help the compiler; that padding business are simply processor
    requirements, i.e. some processors can only load/store 4 byte ints on 4 byte
    address boundaries (if you don't do that you get a 'bus error').

    3) See 2)

    kind regards,

    Jos

    ps micro controllers (for embedded stuff) are most of the times easier on padding.

    Comment

    • wildc
      New Member
      • Mar 2008
      • 4

      #3
      So you are saying that it is not possible to restrict padding because the processor can only load/store fixed amout of bytes????????

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by wildc
        So you are saying that it is not possible to restrict padding because the processor can only load/store fixed amout of bytes????????
        As I wrote: it depends on the processor; some processors can handle different
        models of (internal or external) padding at the cost of a few clock cycles; others
        can handle only one model etc. etc.

        kind regards,

        Jos

        Comment

        Working...