Memory Padding and alignment

P

Paul_Huang

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
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
 
G

Gianni Mariani

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 !
 
S

Serge Paccalin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top