Monday, December 16, 2024

Data Structure Alignment

The C++ compiler aligns data structures 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 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).

/* 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;
}
view raw alignment.cpp hosted with ❤ by GitHub

Field Offsets and Padding

  1. int i1:

    • Requires 4-byte alignment.
    • Starts at offset 0.
    • Takes 4 bytes.
    • The next field, d1, requires 8-byte alighment. Since i1 ends at offset 4, the compiler adds 4 bytes of padding after i1.
  2. double d1:

    • Requires 8-byte alignment.
    • Starts at offset 4 + 4 = 8.
    • Takes 8 bytes.
  3. char s[9]:

    • Requires no specific alignment (1-byte alignment is sufficient).
    • Starts at offset 16 (immediately after d1).
    • Takes 9 bytes.
    • The next field, int i2, requires 4-byte alignment. Therefore, the compiler adds 3 bytes of padding after s to ensure proper alignment.
  4. int i2:

    • Requires 4-byte alignment.
    • Starts at offset 28.
    • Takes 4 bytes.
  5. double d2:

    • Requires 8-byte alignment.
    • The next offset must be a multiple of 8. Since i2 ends at offset 32 (already aligned), no padding is required.
    • Starts at offset 32.
    • Takes 8 bytes.

No comments:

Post a Comment