# 恐慌：运行时错误：未对齐的内存访问

- **ID:** `go/cgo-struct-alignment`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

将具有不同对齐要求的Go结构体传递给C，在某些架构（如ARM）上导致未对齐访问。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.19 | active | — | — |
| 1.20 | active | — | — |

## 解决方案

1. **Ensure Go struct has same alignment as C using `unsafe.Alignof`** (80% 成功率)
   ```
   type MyStruct struct {
    A int32
    B int64
}
_ = unsafe.Alignof(MyStruct{}) // check alignment
   ```
2. **Use `C.struct_MyStruct` directly in Go** (90% 成功率)
   ```
   // #include <mylib.h>
// type GoStruct C.struct_MyStruct
   ```

## 无效尝试

- **Adding `// #pragma pack(1)` in C code** — Go struct may still be aligned differently. (50% 失败率)
- **Ignoring the panic and hoping for the best** — Will crash on strict alignment platforms. (100% 失败率)
