{
  "id": "go/cgo-struct-alignment",
  "signature": "panic: runtime error: unaligned memory access",
  "signature_zh": "恐慌：运行时错误：未对齐的内存访问",
  "regex": "panic:\\ runtime\\ error:\\ unaligned\\ memory\\ access",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Passing a Go struct with different alignment requirements to C, causing unaligned access on some architectures (e.g., ARM).",
  "root_cause_type": "generic",
  "root_cause_zh": "将具有不同对齐要求的Go结构体传递给C，在某些架构（如ARM）上导致未对齐访问。",
  "versions": [
    {
      "version": "1.19",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Adding `// #pragma pack(1)` in C code",
      "why_fails": "Go struct may still be aligned differently.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the panic and hoping for the best",
      "why_fails": "Will crash on strict alignment platforms.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure Go struct has same alignment as C using `unsafe.Alignof`",
      "success_rate": 0.8,
      "how": "type MyStruct struct {\n    A int32\n    B int64\n}\n_ = unsafe.Alignof(MyStruct{}) // check alignment",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `C.struct_MyStruct` directly in Go",
      "success_rate": 0.9,
      "how": "// #include <mylib.h>\n// type GoStruct C.struct_MyStruct",
      "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.82,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-12-03",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}