Language C++----Questions and Answers---Page 6
Question - Why isn't sizeof for a struct equal to the sum of sizeof of each member?
Answer -
This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs.
Here's an example using typical settings for an x86 processor (all used 32 and 64 bit modes):
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
For example, a pointer by default reside on 4-byte boundaries for efficiency, i.e. its address must be a multiple of 4. If the struct contains only a char and a pointer
Answer -
This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs.
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(X); /* = 12 */
const int sizeY = sizeof(Y); /* = 8 */
const int sizeZ = sizeof(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.For example, a pointer by default reside on 4-byte boundaries for efficiency, i.e. its address must be a multiple of 4. If the struct contains only a char and a pointer
struct {
char a;
void* b;
};
then b
cannot use the adderss #1 — it must be placed at #4. 0 1 2 3 4 5 6 7
+---+- - - - - -+---------------+
| a | (unused) | b |
+---+- - - - - -+---------------+
In this case,
struct struct_type{
int i;
char ch;
int *p;
double d;
} s;
the extra 7 bytes comes from 3 bytes due to alignment of int*
, and 4 bytes due to alignment of double
. 0 1 2 3 4 5 6 7
+-------------------+----+-----------+
| i | ch | (unused) |
+-------------------+----+-----------+
8 9 10 11 12 13 14 15
+-------------------+----------------+
| p | (unused) |
+-------------------+----------------+
16 17 18 19 20 21 22 23
+------------------------------------+
| d |
+------------------------------------+
Comments
Post a Comment