Memory Padding and alignment

Discussion in 'C++' started by Paul_Huang, Sep 21, 2004.

  1. Paul_Huang

    Paul_Huang Guest

    OK, I tried it with a piece of sample code to test the memory padding and
    alignment and get some weird results. I would appreciate if anybody can
    help to give a explain.

    Below is my sample code:
    ***************************************************
    #include <iostream>
    using namespace std;
    #include <Windows.h>

    #pragma pack( push )
    #pragma pack( 2 )

    struct GroupEntry2
    {
    BYTE bWidth; // Width, in pixels, of the image
    BYTE bHeight; // Height, in pixels, of the image
    BYTE bColorCount; // Number of colors in image (0 if
    >=8bpp)

    BYTE bReserved; // Reserved
    WORD wPlanes; // Color Planes
    WORD wBitCount; // Bits per pixel
    DWORD dwBytesInRes; // how many bytes in this resource?
    };

    struct DllGroupEntry2 : GroupEntry2
    {
    WORD nID; // the ID
    };

    struct FileGroupEntry2 : GroupEntry2
    {
    DWORD dwImageOffset;
    };

    struct Group2
    {
    WORD zero;
    WORD idType; // Resource type (1 for icons, 2 for cursors)
    WORD idCount; // How many images?
    };

    #pragma pack( pop )

    struct GroupEntry
    {
    BYTE bWidth; // Width, in pixels, of the image
    BYTE bHeight; // Height, in pixels, of the image
    BYTE bColorCount; // Number of colors in image (0
    if>=8bpp)
    BYTE bReserved; // Reserved
    WORD wPlanes; // Color Planes
    WORD wBitCount; // Bits per pixel
    DWORD dwBytesInRes; // how many bytes in this resource?
    };

    struct DllGroupEntry : GroupEntry
    {
    WORD nID; // the ID
    };

    struct FileGroupEntry : GroupEntry
    {
    DWORD dwImageOffset;
    };

    struct Group
    {
    WORD zero;
    WORD idType; // Resource type (1 for icons, 2 for cursors)
    WORD idCount; // How many images?
    };


    void main() {
    cout<<"sizeof GroupEntry2 = "<<sizeof(GroupEntry2)<<endl;
    cout<<"sizeof DllGroupEntry2 = "<<sizeof(DllGroupEntry2)<<endl;
    cout<<"sizeof FileGroupEntry2 = "<<sizeof(FileGroupEntry2)<<endl;
    cout<<"sizeof Group2 = "<<sizeof(Group2)<<endl;

    cout<<"sizeof GroupEntry = "<<sizeof(GroupEntry)<<endl;
    cout<<"sizeof DllGroupEntry = "<<sizeof(DllGroupEntry)<<endl;
    cout<<"sizeof FileGroupEntry = "<<sizeof(FileGroupEntry)<<endl;
    cout<<"sizeof Group = "<<sizeof(Group)<<endl;
    }
    **************************************************
    I compiled and run the code. below is the output:
    --------------------------------------------------
    sizeof GroupEntry2 = 12
    sizeof DllGroupEntry2 = 14
    sizeof FileGroupEntry2 = 16
    sizeof Group2 = 6
    sizeof GroupEntry = 12
    sizeof DllGroupEntry = 16
    sizeof FileGroupEntry = 16
    sizeof Group = 6
    -------------------------------------------
    My environment is: Windows XP on Pentium 4, VC++ 6.0.

    My question is:
    1. Why the size of DllGroupEntry is 16? Is it due to the memory alignment
    to 4 bytes?
    2. If the answer to the above question is "true", why the size of Group is
    still 6 instead of 8? Why the structure of Group is not aligned to 8
    bytes? Why not insert 2 bytes into Group for the memory alignment?

    Thanks
    Paul_Huang, Sep 21, 2004
    #1
    1. Advertising

  2. Re: Memory Padding and alignment [OT]

    BTW - this is all off topic to C++ - you'll get better answers from a
    platform specific group.


    Paul_Huang wrote:

    ....

    >
    > struct GroupEntry
    > {
    > BYTE bWidth; // Width, in pixels, of the image

    1
    > BYTE bHeight; // Height, in pixels, of the image

    2
    > BYTE bColorCount; // Number of colors in image (0
    > if>=8bpp)

    3
    > BYTE bReserved; // Reserved

    4
    > WORD wPlanes; // Color Planes

    6
    > WORD wBitCount; // Bits per pixel

    8
    > DWORD dwBytesInRes; // how many bytes in this resource?

    12
    .... alignment of GroupEntry is constrained by dwBytesInRes which is 4.
    > };
    >
    > struct DllGroupEntry : GroupEntry
    > {

    12
    > WORD nID; // the ID

    14
    .... Aligment is constrained by GroupEntry which is 4
    (need to add 2 bytes).
    16
    > };
    >

    ....
    >
    > My question is:
    > 1. Why the size of DllGroupEntry is 16? Is it due to the memory alignment
    > to 4 bytes?


    because of the DWORD member ...

    > 2. If the answer to the above question is "true", why the size of Group is
    > still 6 instead of 8?


    because it has no members that require more than 2 byte alignment.

    Why the structure of Group is not aligned to 8

    Sam answer.

    > bytes? Why not insert 2 bytes into Group for the memory alignment?


    Because no member variables require it !

    >
    > Thanks
    >
    Gianni Mariani, Sep 21, 2004
    #2
    1. Advertising

  3. Le mardi 21 septembre 2004 à 03:27:43, Paul_Huang a écrit dans
    comp.lang.c++ :

    > OK, I tried it with a piece of sample code to test the memory padding and
    > alignment and get some weird results. I would appreciate if anybody can
    > help to give a explain.
    >
    > Below is my sample code:
    > ***************************************************


    [snip]

    > struct GroupEntry
    > {
    > BYTE bWidth; // Width, in pixels, of the image
    > BYTE bHeight; // Height, in pixels, of the image
    > BYTE bColorCount; // Number of colors in image (0
    > if>=8bpp)
    > BYTE bReserved; // Reserved
    > WORD wPlanes; // Color Planes
    > WORD wBitCount; // Bits per pixel
    > DWORD dwBytesInRes; // how many bytes in this resource?
    > };
    >
    > struct DllGroupEntry : GroupEntry
    > {
    > WORD nID; // the ID
    > };
    >
    > struct FileGroupEntry : GroupEntry
    > {
    > DWORD dwImageOffset;
    > };
    >
    > struct Group
    > {
    > WORD zero;
    > WORD idType; // Resource type (1 for icons, 2 for cursors)
    > WORD idCount; // How many images?
    > };


    [snip]

    > sizeof GroupEntry = 12
    > sizeof DllGroupEntry = 16
    > sizeof FileGroupEntry = 16
    > sizeof Group = 6
    > -------------------------------------------
    > My environment is: Windows XP on Pentium 4, VC++ 6.0.
    >
    > My question is:
    > 1. Why the size of DllGroupEntry is 16? Is it due to the memory alignment
    > to 4 bytes?


    It's due to the requirement (specific to your environment) that DWORDs
    be aligned on 32-bits; 'DllGroupEngry' (by inheritance) has a DWORD
    ('dwBytesInRes').

    > 2. If the answer to the above question is "true", why the size of Group is
    > still 6 instead of 8? Why the structure of Group is not aligned to 8
    > bytes? Why not insert 2 bytes into Group for the memory alignment?


    'Group' only has WORDs with a 16-bit alignment requirement.

    --
    ___________ 2004-09-21 08:00:14
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
    Serge Paccalin, Sep 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ninan Thomas
    Replies:
    3
    Views:
    405
    Kevin Goodsell
    Aug 22, 2003
  2. SenderX
    Replies:
    2
    Views:
    863
    perry
    May 13, 2004
  3. Ninan Thomas
    Replies:
    3
    Views:
    1,883
    Kevin Goodsell
    Aug 22, 2003
  4. Spoon

    Padding and alignment

    Spoon, Feb 12, 2007, in forum: C++
    Replies:
    12
    Views:
    590
    Rolf Magnus
    Feb 13, 2007
  5. Andrew Tomazos
    Replies:
    7
    Views:
    840
    Richard Herring
    Jun 24, 2009
Loading...

Share This Page