Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory Padding and alignment

Reply
Thread Tools

Memory Padding and alignment

 
 
Paul_Huang
Guest
Posts: n/a
 
      09-21-2004

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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-21-2004

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
>

 
Reply With Quote
 
 
 
 
Serge Paccalin
Guest
Posts: n/a
 
      09-21-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
std::vector padding behavior and alignment Andrew Tomazos C++ 7 06-24-2009 11:17 AM
Padding and alignment Spoon C++ 12 02-13-2007 09:31 AM
Allocation schema w.r.t. padding and alignment... SenderX C++ 2 05-13-2004 08:51 PM
memcpy problem with padding for word alignment!!! very urgent Ninan Thomas C++ 3 08-22-2003 06:19 AM
memcpy problem with padding for word alignment!!! very urgent Ninan Thomas C Programming 3 08-22-2003 06:19 AM



Advertisments