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