# 恐慌：运行时错误：无效的内存地址或空指针解引用

- **ID:** `go/unsafe-pointer-arithmetic`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

错误地使用`unsafe.Pointer`算术，导致访问越界内存。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## 解决方案

1. **Use `unsafe.Add` for safe pointer arithmetic (Go 1.17+)** (90% 成功率)
   ```
   p := unsafe.Pointer(&arr[0])
next := unsafe.Add(p, unsafe.Sizeof(arr[0])*2)
_ = *(*int)(next)
   ```
2. **Convert to `uintptr` only immediately before use and pin** (75% 成功率)
   ```
   runtime.KeepAlive(arr)
ptr := uintptr(unsafe.Pointer(&arr[0])) + unsafe.Sizeof(arr[0])
_ = *(*int)(unsafe.Pointer(ptr))
   ```

## 无效尝试

- **Adding offset without aligning to type size** — Unaligned access can cause crash on some architectures. (70% 失败率)
- **Using `uintptr` as pointer without pinning** — GC may move the object, making uintptr stale. (80% 失败率)
