Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C++

Structure Padding

Rate me:
Please Sign up or sign in to vote.
4.32/5 (18 votes)
23 Dec 2009CPOL3 min read 47.6K   19   9
An article on structure padding.

Introduction

The size of a class can be changed simply by playing with the order of its member declarations. Most processors require specific memory alignment on variables of certain types.

Alignment rules

  1. bool variables can be byte aligned and appear at any byte boundary.
  2. short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0x1005 is not a valid location for a short variable, but 0x1006 is.
  3. int (4 byte) variables must be 4 byte aligned, they can only appear at byte boundaries that are a multiple of 4 bytes. This means that 0x1006 is not a valid location for a long variable, but 0x1008 is.

So, structure padding occurs because the members of a structure must appear at the correct byte boundary. To achieve this, the compiler puts in padding so that the structure members appear in the correct location. And the size of the structure should be aligned to the memory so that the padding will occur at the end of the structure too.

Example:

C#
struct IMAGE_INFO
{
  bool        bFlag1;
  unsigned short  usNumber;
  bool        bFlag2;
  int            nNumber;
  bool        bFlag3;
};

1004

1005

1006

1007

1008

1009

100A

100B

100C

100D

100E

100F

1010

1011

1012

1013

BOOL

PAD

--SHORT--

BOOL

PAD

PAD

PAD

---------INT--------

BOOL

PAD

PAD

PAD

In this example, the alignment is as follows:

  • bFlag1 (bool) can appear at any byte boundary (as per rule 1). So, imagine that the memory allocation for the structure IMAGE_INFO starts at location 0x1004 and the first member bFlag1 takes up that location for its 1 byte size storage.
  • usNumber (short) should have its first byte appearing at an even location. So it can not take up the location 0x1005. It will take the locations 0x1006 and 0x1007. The location 0x1005 is padded (as per rule 2).
  • bFlag2 (bool) can then appear in the next available memory location. So it occupies 0x1008 (as per rule 1).
  • nNumber (int) must start at a location which is the multiple of 4. The next possible location for nNumber is 0x100C. So, there are 3 padding bytes between bool and int (as per rule 3).
  • bGSPSStatus (bool) then appears in the available memory location; however, because the structure contains an int member, the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore, there are 3 padding bytes at the end of the structure.

The structure would be 16 bytes long.

For optimization, the structure can be re-written like this:

C#
struct IMAGE_INFO
{
 int              nNumber;
 unsigned short   usNumber;
 bool             bFlag1;
 bool             bFlag2;
 bool             bFlag3;
};

1004

1005

1006

1007

1008

1009

100A

100B

100C

100D

100E

100F

-----------INT------------

---SHORT---

BOOL

BOOL

BOOL

PAD

PAD

PAD

The int appears at the correct byte alignment. The short will be correctly aligned, so no need for padding between the int and short. The three bools can appear at any location. The structure must be a multiple of 4 bytes in size since it contains an int, so 3 padding bytes appear after the last bool. Now, it is only 12 bytes long. So we saved 4 bytes by proper alignment of variables.

How to avoid structure padding using pragma?

The default compiler alignment is of 4 bytes. We need to change it to 1 byte.

For that, do the following steps:

  1. Push the current compiler alignment into the stack.
  2. Set the alignment into 1 byte.
  3. Declare the structure.
  4. Restore the default compiler alignment from the stack.

Example:

C#
#pragma pack(push)  /* push current alignment to stack */
#pragma pack(1)     /* set alignment to 1 byte boundary */

struct MyPackedData
{
    char Data1;
    long Data2;
    char Data3;
};
#pragma pack(pop)   /* restore original alignment from stack */

Thus the structure size is 6 bytes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThx a lot Pin
cute_friend707727-Sep-10 23:22
cute_friend707727-Sep-10 23:22 
GeneralNice one machaaa.. Pin
rajajay8218-Jan-10 0:06
rajajay8218-Jan-10 0:06 
GeneralMy vote of 1 Pin
Tim Craig24-Dec-09 13:27
Tim Craig24-Dec-09 13:27 
GeneralRe: My vote of 1 Pin
Aric Wang27-Dec-09 3:39
Aric Wang27-Dec-09 3:39 
GeneralVery Good Pin
John R. Shaw24-Dec-09 13:21
John R. Shaw24-Dec-09 13:21 
GeneralI think it is better to post this a tip/trick Pin
Adam Roderick J23-Dec-09 23:47
Adam Roderick J23-Dec-09 23:47 
GeneralNot very informative Pin
Ajay Vijayvargiya23-Dec-09 19:39
Ajay Vijayvargiya23-Dec-09 19:39 
GeneralNice Pin
Attila Kúr23-Dec-09 9:41
Attila Kúr23-Dec-09 9:41 
GeneralRe: Nice Pin
Febil Chacko Thanikal23-Dec-09 16:15
Febil Chacko Thanikal23-Dec-09 16:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.