Why isn't sizeof for a struct equal to the sum of sizeof of each member?
This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs:
- Mis-aligned access might be a hard error (often
SIGBUS
). - Mis-aligned access might be a soft error.
- Either corrected in hardware, for a modest performance-degradation.
- Or corrected by emulation in software, for a severe performance-degradation.
- In addition, atomicity and other concurrency-guarantees might be broken, leading to subtle errors.
Here's an example using typical settings for an x86 processor (all used 32 and 64 bit modes):
struct X
{
short s; /* 2 bytes */
/* 2 padding bytes */
int i; /* 4 bytes */
char c; /* 1 byte */
/* 3 padding bytes */
};
struct Y
{
int i; /* 4 bytes */
char c; /* 1 byte */
/* 1 padding byte */
short s; /* 2 bytes */
};
struct Z
{
int i; /* 4 bytes */
short s; /* 2 bytes */
char c; /* 1 byte */
/* 1 padding byte */
};
const int sizeX = sizeof(struct X); /* = 12 */
const int sizeY = sizeof(struct Y); /* = 8 */
const int sizeZ = sizeof(struct Z); /* = 8 */
One can minimize the size of structures by sorting members by alignment (sorting by size suffices for that in basic types) (like structure Z
in the example above).
IMPORTANT NOTE: Both the C and C++ standards state that structure alignment is implementation-defined. Therefore each compiler may choose to align data differently, resulting in different and incompatible data layouts. For this reason, when dealing with libraries that will be used by different compilers, it is important to understand how the compilers align data. Some compilers have command-line settings and/or special #pragma
statements to change the structure alignment settings.
Why doesn't size of struct is equals to sum of sizes of its individual member types? [duplicate]
It is because the compiler uses padding to bring each element into word alignment that is specific to the architecture for which you are compiling.
It can be for one of several reasons but usually:
Because some CPU's simply cannot read a
long or long longmulti-byte value when it isn't on an address multiple of its own size.Because CPU's that can read off-aligned data may do it much slower than aligned.
You can often force it off with a compiler-specific directive or pragma.
When you do this, the compiler will generate relatively inefficient code to access the off-aligned data using multiple read/write operations.
Why is the sizeof of a structure larger than the sum of its parts? [duplicate]
It's to demonstrate padding issues done by the compiler. What it (the compiler) did here was to align each part of the struct to 4 byte word boundaries (=> 4*3 = 12 bytes) instead of packing them into 9 bytes. This is performed by the compiler to allow for data member access that respects the target CPU memory access patterns.
Related Topics
How to Peek At the Next Element in a Range-For Loop
What Is the Most Effective Way For Float and Double Comparison
Capture Characters from Standard Input Without Waiting For Enter to Be Pressed
Parse (Split) a String in C++ Using String Delimiter (Standard C++)
How Do Malloc() and Free() Work
Why Is Unsigned Integer Overflow Defined Behavior But Signed Integer Overflow Isn'T
What Is Std::Move(), and When Should It Be Used
Are Std::Vector Elements Guaranteed to Be Contiguous
Explicit Template Instantiation - When Is It Used
Vector Converted All Negative Values to Zero
What Are the Basic Rules and Idioms For Operator Overloading
When and Why Will a Compiler Initialise Memory to 0Xcd, 0Xdd, etc. on Malloc/Free/New/Delete
Undefined Reference to 'Winmain@16'
What Is a "Translation Unit" in C++
C++ Unordered_Map Using a Custom Class Type as the Key