在 64 位系统中:
type EpollEvent struct {
Events uint32
Fd int32
Pad int32
}
C 结构是
struct epoll_event {
uint32_t events;
uint64_t data; // union ,反正大小是固定 8B
};
由于内存对齐,events 、data 中间有 4B 的空余空间,刚好对应在 go 结构中的 Fd 字段上 ?
![]() |
1
lysShub OP 没问题的,原来是 C 用 __attribute__((packed)) 消除了内存对齐
|
2
patrickyoung 20 天前 via Android
RTFM:
> For a variable x of struct type: unsafe.Alignof(x) is the largest of all the values unsafe.Alignof(x.f) for each field f of x, but at least 1. https://go.dev/ref/spec#Size_and_alignment_guarantees |