go runtime_error ai_generated true

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

panic: runtime error: unaligned memory access

ID: go/cgo-struct-alignment

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2024-12-03首次发现

版本兼容性

版本状态引入弃用备注
1.19 active
1.20 active

根因分析

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

English

Passing a Go struct with different alignment requirements to C, causing unaligned access on some architectures (e.g., ARM).

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Adding `// #pragma pack(1)` in C code 50% 失败

    Go struct may still be aligned differently.

  2. Ignoring the panic and hoping for the best 100% 失败

    Will crash on strict alignment platforms.