go
runtime_error
ai_generated
true
panic: runtime error: unaligned memory access
ID: go/cgo-struct-alignment
80%Fix Rate
82%Confidence
0Evidence
2024-12-03First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.19 | active | — | — | — |
| 1.20 | active | — | — | — |
Root Cause
Passing a Go struct with different alignment requirements to C, causing unaligned access on some architectures (e.g., ARM).
generic中文
将具有不同对齐要求的Go结构体传递给C,在某些架构(如ARM)上导致未对齐访问。
Workarounds
-
80% success Ensure Go struct has same alignment as C using `unsafe.Alignof`
type MyStruct struct { A int32 B int64 } _ = unsafe.Alignof(MyStruct{}) // check alignment -
90% success Use `C.struct_MyStruct` directly in Go
// #include <mylib.h> // type GoStruct C.struct_MyStruct
Dead Ends
Common approaches that don't work:
-
Adding `// #pragma pack(1)` in C code
50% fail
Go struct may still be aligned differently.
-
Ignoring the panic and hoping for the best
100% fail
Will crash on strict alignment platforms.