go unix.EpollEvent 它这个结构有问题吧?

26 天前
 lysShub

在 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 字段上 ?

1215 次点击
所在节点    Go 编程语言
2 条回复
lysShub
26 天前
没问题的,原来是 C 用 __attribute__((packed)) 消除了内存对齐
patrickyoung
26 天前
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1152243

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX