473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Memory Padding and alignment


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

Jul 22 '05 #1
2 3358

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

Jul 22 '05 #2
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: serikas | last post by:
Is there a way to get aligned dynamically allocated memory? (provided that the requested memory size is a power of 2.) For example, if I request 128 bytes of memory, can I implement an allocator...
5
by: achrist | last post by:
Here's a program: #include <stdio.h> int main(int argc, char **argv) { double an_array = {0.0, 1.0, 2.0, 3.0, 4.0}; typedef struct a_struct {double v0, v1, v2,v3, v4;} a_type; typedef...
6
by: Everton da Silva Marques | last post by:
Hi, I need to allocate, using malloc(), a single memory block to hold two structures and a variable length string. Is it safe (portable, alignment-wise) to sum up the sizeof's of those...
1
by: rahul8143 | last post by:
hello, What is mean by memory alignment in context of structure padding? also why structure padding is useful? is it implicitly done by latest c,c++ compilers or i have to specify it explicitly?...
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
8
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I heard when define a struct, it is important to make the size of the struct memory aligned. For example, struct foo
14
by: barcaroller | last post by:
I have a multi-field struct and a memory block (created using malloc) that contains structured data. If I map the struct to the memory block, am I guaranteed that the struct fields will be filled...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.