double
). This ensures faster memory access, as modern CPUs perform better when data is aligned to specific boundaries because it results in single memory word access. As a side effect, sizeof(MyStructure)
(40 bytes due to padding) is larger than the sum of individual fields (33 bytes).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The compiler aligns the structure to the largest alignment required by any field (8 bytes in the case below, due to double). | |
This ensures faster memory access, as modern CPUs perform better when data is aligned to specific boundaries because of fewer | |
memory access. | |
Şamil Korkmaz, 16.12.2024 */ | |
#include <iostream> | |
struct MyStruct { | |
int i1; // 4 bytes | |
double d1; // 8 bytes | |
char s[9]; // 9 bytes | |
int i2; // 4 bytes | |
double d2; // 8 bytes | |
}; // sum: 4+8+9+4+8 = 33 bytes | |
int main() { | |
std::cout << "Offset of i1: " << offsetof(MyStruct, i1) << "\n"; // 0 | |
std::cout << "Offset of d1: " << offsetof(MyStruct, d1) << "\n"; // 8 | |
std::cout << "Offset of s : " << offsetof(MyStruct, s) << "\n"; // 16 | |
std::cout << "Offset of i2: " << offsetof(MyStruct, i2) << "\n"; // 28 | |
std::cout << "Offset of d2: " << offsetof(MyStruct, d2) << "\n"; // 32 | |
std::cout << "sizeof(MyStruct): " << sizeof(MyStruct) << " bytes\n"; // 40 | |
return 0; | |
} |
Field Offsets and Padding
int i1
:- Requires 4-byte alignment.
- Starts at offset
0
. - Takes
4 bytes
. - The next field,
d1
, requires 8-byte alighment. Sincei1
ends at offset4
, the compiler adds 4 bytes of padding afteri1
.
double d1
:- Requires 8-byte alignment.
- Starts at offset
4 + 4 = 8
. - Takes
8 bytes
.
char s[9]
:- Requires no specific alignment (1-byte alignment is sufficient).
- Starts at offset
16
(immediately afterd1
). - Takes
9 bytes
. - The next field,
int i2
, requires 4-byte alignment. Therefore, the compiler adds 3 bytes of padding afters
to ensure proper alignment.
int i2
:- Requires 4-byte alignment.
- Starts at offset
28
. - Takes
4 bytes
.
double d2
:- Requires 8-byte alignment.
- The next offset must be a multiple of 8. Since
i2
ends at offset32
(already aligned), no padding is required. - Starts at offset
32
. - Takes
8 bytes
.
No comments:
Post a Comment