structure padding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ramu

    structure padding

    Hi all,
    I understand that some compilers pad some bytes to the
    aggregate data types in order to access the members of the aggregate
    data types(i.e. structure) fast. This depends on the architectures.
    Some architectures cannot access the data which will be stored on the
    odd addresses or they may find difficult to access it. This is the
    reason for padding extra bytes.

    Now consider the following structure:

    struct {
    int i; // 4 bytes
    char c; // 1 byte
    char b; // 1 byte
    }a;

    Now consider the memory representation for this structure:

    ----------------------------- Memory locations
    i 0
    -----------------------------
    i 1
    -----------------------------
    i 2
    ------------------------------
    i 3
    -------------------------------
    c 4
    --------------------------------
    b 5
    --------------------------------
    padding 6
    ---------------------------------
    padding 7
    ---------------------------------

    The size of this strucutre will be 8bytes after padding.
    The first 4 memory locations(0 to 3) are allocated for int i;
    The next byte(4) is allocated for char c;
    the next byte(5) is allocated for char b;

    Now my doubt is , does not the processor find it difficult to access
    the value of the variable which is stored in odd memory location(ie.
    5)? can anyone elaborate on this?

    Regards

  • Vladimir S. Oka

    #2
    Re: structure padding

    ramu wrote:
    [color=blue]
    > Hi all,
    > I understand that some compilers pad some bytes to the
    > aggregate data types in order to access the members of the aggregate
    > data types(i.e. structure) fast. This depends on the architectures.
    > Some architectures cannot access the data which will be stored on the
    > odd addresses or they may find difficult to access it. This is the
    > reason for padding extra bytes.[/color]

    This is off topic here.
    [color=blue]
    > Now consider the following structure:
    >
    > struct {
    > int i; // 4 bytes
    > char c; // 1 byte
    > char b; // 1 byte
    > }a;
    >
    > Now consider the memory representation for this structure:[/color]

    < snip >

    <OT>
    If such a padding is to take place, it's more likely that both `c` and
    `d` will `occupy` a 4-byte memory block, or whichever block is
    convenient for the implementation.
    </OT>

    Cheers

    Vladimir

    --
    "Stealing a rhinoceros should not be attempted lightly."

    Comment

    • santosh

      #3
      Re: structure padding

      In 32 - bit processors , Each bus cycle will access the 32 - bit data
      from memory. For reading variable b, first the processor reads the
      entire 32 bit word which includes b, c and the padded data.

      This is the reason why padding is required. (it is required for
      allignment).

      Comment

      • Nelu

        #4
        Re: structure padding

        santosh wrote:[color=blue]
        > In 32 - bit processors , Each bus cycle will access the 32 - bit data
        > from memory. For reading variable b, first the processor reads the
        > entire 32 bit word which includes b, c and the padded data.
        >
        > This is the reason why padding is required. (it is required for
        > allignment).
        >[/color]
        Before posting again, please read:


        Thank you.

        --
        Ioan - Ciprian Tandau
        tandau _at_ freeshell _dot_ org (hope it's not too late)
        (... and that it still works...)

        Comment

        • Rod Pemberton

          #5
          Re: structure padding


          "ramu" <ramu.ask@gmail .com> wrote in message
          news:1138862197 .556586.94630@g 47g2000cwa.goog legroups.com...[color=blue]
          > Hi all,
          > I understand that some compilers pad some bytes to the
          > aggregate data types in order to access the members of the aggregate
          > data types(i.e. structure) fast. This depends on the architectures.
          > Some architectures cannot access the data which will be stored on the
          > odd addresses or they may find difficult to access it. This is the
          > reason for padding extra bytes.
          >
          > Now consider the following structure:
          >
          > struct {
          > int i; // 4 bytes
          > char c; // 1 byte
          > char b; // 1 byte
          > }a;
          >[/color]

          Okay. Layout can be compiler specific.
          [color=blue]
          > Now my doubt is , does not the processor find it difficult to access
          > the value of the variable which is stored in odd memory location(ie.
          > 5)? can anyone elaborate on this?[/color]

          No idea. The code generate by DJGPP and OW are radically different. You'll
          need to study the disassembly.


          If you want to see the exact layout of a structure with different packings,
          you can play with this code:

          #include <stdio.h>

          // Works for DJGPP 2.03 and OW 1.3
          #ifdef __DJGPP__
          #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
          #endif

          // default alignment
          struct {
          unsigned int i;
          unsigned char c;
          unsigned long a;
          unsigned short e;
          unsigned char b;
          } s0;

          #pragma pack(push,1)
          struct {
          unsigned int i;
          unsigned char c;
          unsigned long a;
          unsigned short e;
          unsigned char b;
          } s1;
          #pragma pack(pop)

          #pragma pack(push,2)
          struct {
          unsigned int i;
          unsigned char c;
          unsigned long a;
          unsigned short e;
          unsigned char b;
          } s2;
          #pragma pack(pop)

          #pragma pack(push,4)
          struct {
          unsigned int i;
          unsigned char c;
          unsigned long a;
          unsigned short e;
          unsigned char b;
          } s3;
          #pragma pack(pop)


          void print_s(unsigne d char *s, int size)
          {
          int i;

          printf("%d ",size);
          for(i=0;i<size; i++)
          {
          printf("%02x ",s[i]);
          }
          printf("\n");
          }

          int main(void)
          {
          s0.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
          s0.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
          s0.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
          s0.c=0xFC;
          s0.b=0xFE;

          s1.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
          s1.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
          s1.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
          s1.c=0xFC;
          s1.b=0xFE;

          s2.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
          s2.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
          s2.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
          s2.c=0xFC;
          s2.b=0xFE;

          s3.i=(unsigned int)0xF0F1F2F3; // cast in case 16 not 32
          s3.a=(unsigned long)0xE0E1E2E3 ; // cast in case 16 not 32
          s3.e=(unsigned short)0xD0D1D2D 3; // cast in case 16 not 32
          s3.c=0xFC;
          s3.b=0xFE;

          printf("s0 ");
          print_s((unsign ed char *)&s0,sizeof(s0 ));
          printf("s1 ");
          print_s((unsign ed char *)&s1,sizeof(s1 ));
          printf("s2 ");
          print_s((unsign ed char *)&s2,sizeof(s2 ));
          printf("s3 ");
          print_s((unsign ed char *)&s3,sizeof(s3 ));

          return(0);
          }


          Rod Pemberton


          Comment

          • Ian Collins

            #6
            Re: structure padding

            santosh wrote:[color=blue]
            > In 32 - bit processors , Each bus cycle will access the 32 - bit data
            > from memory. For reading variable b, first the processor reads the
            > entire 32 bit word which includes b, c and the padded data.
            >
            > This is the reason why padding is required. (it is required for
            > allignment).
            >[/color]
            It isn't required (but is desirable) on processors that can perform
            misaligned access.

            Alignment is very implementation specific and can be a compatibility
            headache if code assumes a certain alignment.

            --
            Ian Collins.

            Comment

            • santosh

              #7
              Re: structure padding

              I dint know that.
              Thanks.


              Nelu wrote:[color=blue]
              > santosh wrote:[color=green]
              > > In 32 - bit processors , Each bus cycle will access the 32 - bit data
              > > from memory. For reading variable b, first the processor reads the
              > > entire 32 bit word which includes b, c and the padded data.
              > >
              > > This is the reason why padding is required. (it is required for
              > > allignment).
              > >[/color]
              > Before posting again, please read:
              > http://cfaj.freeshell.org/google/
              >
              > Thank you.
              >
              > --
              > Ioan - Ciprian Tandau
              > tandau _at_ freeshell _dot_ org (hope it's not too late)
              > (... and that it still works...)[/color]

              Comment

              • Vladimir S. Oka

                #8
                Re: structure padding

                santosh wrote:[color=blue]
                > Nelu wrote:[color=green]
                > > santosh wrote:[color=darkred]
                > > > In 32 - bit processors , Each bus cycle will access the 32 - bit data
                > > > from memory. For reading variable b, first the processor reads the
                > > > entire 32 bit word which includes b, c and the padded data.
                > > >
                > > > This is the reason why padding is required. (it is required for
                > > > allignment).
                > > >[/color]
                > > Before posting again, please read:
                > > http://cfaj.freeshell.org/google/[/color]
                >
                > I dint know that.[/color]

                I believe it also says something about top-posting (I corrected yours
                here), and possibly even about not quoting signatures.

                Cheers

                Vladimir

                Comment

                • Robin Haigh

                  #9
                  Re: structure padding


                  "ramu" <ramu.ask@gmail .com> wrote in message
                  news:1138862197 .556586.94630@g 47g2000cwa.goog legroups.com...[color=blue]
                  > Hi all,
                  > I understand that some compilers pad some bytes to the
                  > aggregate data types in order to access the members of the aggregate
                  > data types(i.e. structure) fast. This depends on the architectures.
                  > Some architectures cannot access the data which will be stored on the
                  > odd addresses or they may find difficult to access it. This is the
                  > reason for padding extra bytes.
                  >
                  > Now consider the following structure:
                  >
                  > struct {
                  > int i; // 4 bytes
                  > char c; // 1 byte
                  > char b; // 1 byte
                  > }a;
                  >
                  > [snip]
                  >
                  > The size of this strucutre will be 8bytes after padding.
                  > The first 4 memory locations(0 to 3) are allocated for int i;
                  > The next byte(4) is allocated for char c;
                  > the next byte(5) is allocated for char b;
                  >
                  > Now my doubt is , does not the processor find it difficult to access
                  > the value of the variable which is stored in odd memory location(ie.
                  > 5)? can anyone elaborate on this?[/color]


                  Alignment is a problem when objects lie across boundaries, requiring
                  multiple fetches if they can be fetched at all.

                  The types which are smaller than a word never straddle a boundary. They
                  were provided in C so that you can pack more than one item into a word. The
                  unpacking overhead should be relatively minor, but if you're worried, you
                  can always store your characters in ints. That's your choice: if you choose
                  the "packable" type, it's not the compiler's job to change your mind.

                  --
                  RSH


                  Comment

                  • Richard Bos

                    #10
                    Re: structure padding

                    "Vladimir S. Oka" <novine@btopenw orld.com> wrote:
                    [color=blue]
                    > ramu wrote:
                    >[color=green]
                    > > I understand that some compilers pad some bytes to the
                    > > aggregate data types in order to access the members of the aggregate
                    > > data types(i.e. structure) fast. This depends on the architectures.
                    > > Some architectures cannot access the data which will be stored on the
                    > > odd addresses or they may find difficult to access it. This is the
                    > > reason for padding extra bytes.[/color]
                    >
                    > This is off topic here.[/color]

                    Not really. That certain types may require alignments, and that
                    structures may contain padding for that and other reasons, is perfectly
                    Standard. Which types happen to require padding on a specific
                    implementation, and how much, that's off topic here. The principle
                    itself, though, is on topic.
                    [color=blue][color=green]
                    > > Now consider the following structure:
                    > >
                    > > struct {
                    > > int i; // 4 bytes
                    > > char c; // 1 byte
                    > > char b; // 1 byte
                    > > }a;
                    > >
                    > > Now consider the memory representation for this structure:[/color]
                    >
                    > < snip >
                    >
                    > <OT>
                    > If such a padding is to take place, it's more likely that both `c` and
                    > `d` will `occupy` a 4-byte memory block, or whichever block is
                    > convenient for the implementation.[/color]

                    Not necessarily. One thing is certain, though: if normal ints aren't
                    just 4 bytes large, but also require 4-byte alignment, the
                    implementation must make certain that no ints, including the one in the
                    second member of an array of these structs, break alignment. Of course
                    it's possible for the implementation to fudge this in the background by
                    accessing all struct-member ints as arrays of char, but the best and
                    perfectly Standard way would be to make the whole struct take 8 bytes: 4
                    for the int, 1 each for the chars, and 2 for padding to make sure the
                    next int also ends up on a well-aligned memory address.

                    Richard

                    Comment

                    • Vladimir S. Oka

                      #11
                      Re: structure padding

                      Richard Bos wrote:[color=blue]
                      > "Vladimir S. Oka" <novine@btopenw orld.com> wrote:
                      >[color=green]
                      > > ramu wrote:
                      > >[color=darkred]
                      > > > I understand that some compilers pad some bytes to the
                      > > > aggregate data types in order to access the members of the aggregate
                      > > > data types(i.e. structure) fast. This depends on the architectures.
                      > > > Some architectures cannot access the data which will be stored on the
                      > > > odd addresses or they may find difficult to access it. This is the
                      > > > reason for padding extra bytes.[/color]
                      > >
                      > > This is off topic here.[/color]
                      >
                      > Not really. That certain types may require alignments, and that
                      > structures may contain padding for that and other reasons, is perfectly
                      > Standard. Which types happen to require padding on a specific
                      > implementation, and how much, that's off topic here. The principle
                      > itself, though, is on topic.[/color]

                      Agreed, in principle. ;-)

                      How OP's query sounded to me, however, was more on the off topic side
                      of it (it asked about how "difficult" it was for the processor). I
                      might have missed the point, though (it has happened before ;-) ).
                      [color=blue][color=green][color=darkred]
                      > > > Now consider the following structure:
                      > > >
                      > > > struct {
                      > > > int i; // 4 bytes
                      > > > char c; // 1 byte
                      > > > char b; // 1 byte
                      > > > }a;
                      > > >
                      > > > Now consider the memory representation for this structure:[/color]
                      > >
                      > > < snip >
                      > >
                      > > <OT>
                      > > If such a padding is to take place, it's more likely that both `c` and
                      > > `d` will `occupy` a 4-byte memory block, or whichever block is
                      > > convenient for the implementation.[/color]
                      >
                      > Not necessarily. One thing is certain, though: if normal ints aren't
                      > just 4 bytes large, but also require 4-byte alignment, the
                      > implementation must make certain that no ints, including the one in the
                      > second member of an array of these structs, break alignment. Of course
                      > it's possible for the implementation to fudge this in the background by
                      > accessing all struct-member ints as arrays of char, but the best and
                      > perfectly Standard way would be to make the whole struct take 8 bytes: 4
                      > for the int, 1 each for the chars, and 2 for padding to make sure the
                      > next int also ends up on a well-aligned memory address.[/color]

                      Well, yes, of course. (still OT)

                      I also said "likely", not "will". It really depends on what one means
                      by `best` (speed, memory efficiency, ...?), and what
                      implementation/architecture we're talking about. Going by OP's
                      specification of a 4-byte int, assuming 4-byte alignment, and
                      architecture that excels in reading 4-bytes at a time, if we want
                      speed, compiler is likely to do what I described. If we asked for
                      memory efficiency, it could do what you suggested. I'm sure one could
                      come up with an architecture where what I just said is not true, but I
                      think it'd have to be quite weird (which still does not make either of
                      us wrong).

                      Cheers

                      Vladimir

                      Comment

                      • Keith Thompson

                        #12
                        Re: structure padding

                        "Rod Pemberton" <dont_have@bitb ucket.cmm> writes:
                        [...][color=blue]
                        > Okay. Layout can be compiler specific.
                        >[color=green]
                        >> Now my doubt is , does not the processor find it difficult to access
                        >> the value of the variable which is stored in odd memory location(ie.
                        >> 5)? can anyone elaborate on this?[/color]
                        >
                        > No idea. The code generate by DJGPP and OW are radically different. You'll
                        > need to study the disassembly.
                        >
                        >
                        > If you want to see the exact layout of a structure with different packings,
                        > you can play with this code:
                        >
                        > #include <stdio.h>
                        >
                        > // Works for DJGPP 2.03 and OW 1.3[/color]
                        [...][color=blue]
                        > #ifdef __DJGPP__
                        > #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
                        > #endif[/color]

                        You never use this macro.
                        [color=blue]
                        > // default alignment
                        > struct {
                        > unsigned int i;
                        > unsigned char c;
                        > unsigned long a;
                        > unsigned short e;
                        > unsigned char b;
                        > } s0;
                        >
                        > #pragma pack(push,1)[/color]
                        [snip]

                        "#pragma pack" is non-standard.

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                        We must do something. This is something. Therefore, we must do this.

                        Comment

                        • Kenneth Brody

                          #13
                          Re: structure padding

                          ramu wrote:
                          [...][color=blue]
                          > Some architectures cannot access the data which will be stored on the
                          > odd addresses or they may find difficult to access it. This is the
                          > reason for padding extra bytes.
                          >
                          > Now consider the following structure:
                          >
                          > struct {
                          > int i; // 4 bytes
                          > char c; // 1 byte
                          > char b; // 1 byte
                          > }a;
                          >
                          > Now consider the memory representation for this structure:
                          >
                          > ----------------------------- Memory locations
                          > i 0
                          > i 1
                          > i 2
                          > i 3
                          > c 4
                          > b 5
                          > padding 6
                          > padding 7
                          > ---------------------------------[/color]
                          [...][color=blue]
                          > Now my doubt is , does not the processor find it difficult to access
                          > the value of the variable which is stored in odd memory location(ie.
                          > 5)? can anyone elaborate on this?[/color]

                          If the processor were unable to access chars at an odd address (highly
                          unlikely, since this would make accessing character arrays difficult),
                          then the compiler could always add additional padding:

                          i: 0, 1, 2, 3
                          c: 4
                          pad: 5
                          d: 6
                          pad: 7

                          More likely, it is 16-bit values which require even addresses, and
                          32-bit values which require either even or multiple-of-4 addresses,
                          while 8-bit chars can be at any (valid) address.

                          (Yes, this assumes 8-bit chars. Adjust the above examples as needed
                          for non-8-bit-char systems.)

                          Also, note that some architectures may allow no padding at all for
                          your example, using only 6 bytes for the struct. Still others may
                          allow it but incur a speed penalty for accessing "unaligned" values.

                          --
                          +-------------------------+--------------------+-----------------------------+
                          | Kenneth J. Brody | www.hvcomputer.com | |
                          | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
                          +-------------------------+--------------------+-----------------------------+
                          Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                          Comment

                          • Rod Pemberton

                            #14
                            Re: structure padding


                            "Keith Thompson" <kst-u@mib.org> wrote in message
                            news:lnhd7hblco .fsf@nuthaus.mi b.org...[color=blue]
                            > "Rod Pemberton" <dont_have@bitb ucket.cmm> writes:
                            > [...][color=green]
                            > > Okay. Layout can be compiler specific.
                            > >[/color][/color]
                            [color=blue][color=green]
                            > > // Works for DJGPP 2.03 and OW 1.3[/color][/color]
                            [color=blue][color=green]
                            > > #ifdef __DJGPP__
                            > > #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
                            > > #endif[/color]
                            >
                            > You never use this macro.[/color]

                            Correct. DJGPP needs it to enable '#pragma pack' and '#pragma push'.
                            [color=blue]
                            >[color=green]
                            > > // default alignment
                            > > struct {
                            > > unsigned int i;
                            > > unsigned char c;
                            > > unsigned long a;
                            > > unsigned short e;
                            > > unsigned char b;
                            > > } s0;
                            > >
                            > > #pragma pack(push,1)[/color]
                            > [snip]
                            >
                            > "#pragma pack" is non-standard.[/color]

                            Correct again. All packing methods are non-standard. This is the only
                            method common to both DJGPP and OpenWATCOM. Both DJGPP and OpenWATCOM have
                            three methods of packing. '...structures. ..' or '...struct data...' will
                            need to be replaced with valid C:

                            /* DJGPP */
                            #ifdef __DJGPP__

                            //DJGPP Method 1
                            //--------
                            #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
                            #pragma pack(push,1)
                            ....structures. ..
                            #pragma pack(pop)

                            //DJGPP Method 2
                            //--------
                            #define HANDLE_SYSV_PRA GMA 1
                            #pragma pack(1)
                            ....structures. ..
                            #pragma pack()

                            //DJGPP Method 3
                            //--------
                            struct EX
                            {
                            ...struct.data. ..
                            } __attribute__ ((packed));

                            #endif

                            /* WATCOM */
                            #ifdef __WATCOMC__

                            //WATCOM Method 1
                            //---------
                            #pragma pack(push,1)
                            ....structures. ..
                            #pragma pack(pop)

                            //WATCOM Method 2
                            //--------
                            #pragma pack(1)
                            ....structures. ..
                            #pragma pack(2)

                            //WATCOM Method 3
                            //--------
                            _Packed struct EX
                            {
                            ...struct.data. ..
                            };

                            #endif


                            Rod Pemberton


                            Comment

                            • Keith Thompson

                              #15
                              Re: structure padding

                              "Rod Pemberton" <dont_have@bitb ucket.cmm> writes:[color=blue]
                              > "Keith Thompson" <kst-u@mib.org> wrote in message
                              > news:lnhd7hblco .fsf@nuthaus.mi b.org...[color=green]
                              >> "Rod Pemberton" <dont_have@bitb ucket.cmm> writes:[/color][/color]
                              [...][color=blue][color=green][color=darkred]
                              >> > #ifdef __DJGPP__
                              >> > #define HANDLE_PRAGMA_P ACK_PUSH_POP 1
                              >> > #endif[/color]
                              >>
                              >> You never use this macro.[/color]
                              >
                              > Correct. DJGPP needs it to enable '#pragma pack' and '#pragma push'.[/color]
                              [...][color=blue][color=green][color=darkred]
                              >> > #pragma pack(push,1)[/color]
                              >> [snip]
                              >>
                              >> "#pragma pack" is non-standard.[/color]
                              >
                              > Correct again. All packing methods are non-standard.[/color]
                              [...]

                              And therefore off-topic.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...