
C/C++
Padding and Rearranging Structure Members
By Dan Saks, September 19, 2012
Exploring memory alignment and the use of padding in structures in C/C++Related Reading
More Insights
White Papers
- Maximize the Human Potential of Your SOC
- Gone Phishing: How to Defend Against Persistent Phishing Attempts Targeting Your Organization
Reports
- State of ITSM in Manufacturing
- Forrester Report: The Total Economic Impact Of Bizagi's Low-Code Intelligent Process Automation Platform
Webcasts
- [FREE Virtual Event] IT Automation in 2023: Enabling Innovation and Efficiency
- Build a Stronger Cybersecurity Defense Virtual Event 5/4

Currently we allow the following HTML tags in comments:
Single tags
These tags can be used alone and don't need an ending tag.
<br>
Defines a single line break
<hr>
Defines a horizontal line
Matching tags
These require an ending tag - e.g. <i>italic text</i>
<a>
Defines an anchor
<b>
Defines bold text
<big>
Defines big text
<blockquote>
Defines a long quotation
<caption>
Defines a table caption
<cite>
Defines a citation
<code>
Defines computer code text
<em>
Defines emphasized text
<fieldset>
Defines a border around elements in a form
<h1>
This is heading 1
<h2>
This is heading 2
<h3>
This is heading 3
<h4>
This is heading 4
<h5>
This is heading 5
<h6>
This is heading 6
<i>
Defines italic text
<p>
Defines a paragraph
<pre>
Defines preformatted text
<q>
Defines a short quotation
<samp>
Defines sample computer code text
<small>
Defines small text
<span>
Defines a section in a document
<s>
Defines strikethrough text
<strike>
Defines strikethrough text
<strong>
Defines strong text
<sub>
Defines subscripted text
<sup>
Defines superscripted text
<u>
Defines underlined text
Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.
![]() |
To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy. |
@Emani:
There's no undefined behavior there. The `#pragma pack` directive tells the compiler to pack the struct so there's no padding (note that all of this is beyond what's required by the standard); since the compiler knows that the offset of `m2` member is (or might be) unaligned, the compiler will generate code that accesses the member in such a way the there is no unaligned access. For example, the compiler may use a series of shifts and byte accesses.
However, the following *would* likely result in undefined behavior:
int* p = &var.m2;
*p = 42;
The compiler should generate at least a warning when assigning the address of an unaligned int member to a plain-old int pointer.
"a program that attempts to access an improperly aligned object produces undefined behavior. This means that the program is in error, but the exact consequences of that error are platform-dependent". Having said this
I would like to know the behavior of following code on a platform which doesn't support improper aligned object access
#pragma pack(push, 1)
typedef struct widget widget;
struct widget
{
char m1;
int m2;
char m3;
};
widget var;
var.m2 = 20; --> accessing int
forget my previous silly question. Each member has to be aligned, so its not sufficient that sizeof is a multiple of 4 (than 8 would be enough)
I do not understand why 6 bytes have to added as trailing padding to the widget structure. 2 more bytes should be sufficient?