{
  "id": "go/unsafe-pointer-arithmetic",
  "signature": "panic: runtime error: invalid memory address or nil pointer dereference",
  "signature_zh": "恐慌：运行时错误：无效的内存地址或空指针解引用",
  "regex": "panic:\\ runtime\\ error:\\ invalid\\ memory\\ address\\ or\\ nil\\ pointer\\ dereference",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Using `unsafe.Pointer` arithmetic incorrectly, resulting in accessing out-of-bounds memory.",
  "root_cause_type": "generic",
  "root_cause_zh": "错误地使用`unsafe.Pointer`算术，导致访问越界内存。",
  "versions": [
    {
      "version": "1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding offset without aligning to type size",
      "why_fails": "Unaligned access can cause crash on some architectures.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Using `uintptr` as pointer without pinning",
      "why_fails": "GC may move the object, making uintptr stale.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use `unsafe.Add` for safe pointer arithmetic (Go 1.17+)",
      "success_rate": 0.9,
      "how": "p := unsafe.Pointer(&arr[0])\nnext := unsafe.Add(p, unsafe.Sizeof(arr[0])*2)\n_ = *(*int)(next)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Convert to `uintptr` only immediately before use and pin",
      "success_rate": 0.75,
      "how": "runtime.KeepAlive(arr)\nptr := uintptr(unsafe.Pointer(&arr[0])) + unsafe.Sizeof(arr[0])\n_ = *(*int)(unsafe.Pointer(ptr))",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}