#include <iostream>
#include <cstring>
#include <iomanip>

// 打印内存内容的工具函数
void hexdump(const void* addr, size_t len) {
    const uint8_t* p = static_cast<const uint8_t*>(addr);
    std::cout << "Address  | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n";
    std::cout << "---------|------------------------------------------------\n";
    for (size_t i = 0; i < len; i += 16) {
        std::cout << reinterpret_cast<const void*>(p + i) << " | ";
        for (int j = 0; j < 16 && (i + j) < len; ++j) {
            std::cout << std::hex << std::setw(2) << std::setfill('0') 
                      << static_cast<int>(p[i + j]) << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    // 分配两个连续的 8 字节块（0x00~0x07 和 0x08~0x0F）
    alignas(8) struct {
        uint8_t block1[8];  // 地址 0x00~0x07
        uint8_t block2[8];   // 地址 0x08~0x0F
    } memory;

    // 染色内存块
    memset(memory.block1, 0xAA, sizeof(memory.block1)); // 块1: 全 0xAA
    memset(memory.block2, 0xBB, sizeof(memory.block2)); // 块2: 全 0xBB

    std::cout << "=== 初始内存状态 ===\n";
    hexdump(&memory, 16);

    // 强制在地址 0x06 写入 int64_t（跨 block1 和 block2）
    volatile int64_t* unaligned_ptr = reinterpret_cast<int64_t*>(
        reinterpret_cast<uint8_t*>(&memory) + 6
    );
    *unaligned_ptr = 0x1122334455667788; // 写入测试值

    std::cout << "\n=== 写入后的内存状态 ===\n";
    hexdump(&memory, 16);

    return 0;
}