V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
lysShub
V2EX  ›  Go 编程语言

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

  •  
  •   lysShub · 20 天前 · 1117 次点击

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

    2 条回复    2025-08-14 08:43:55 +08:00
    lysShub
        1
    lysShub  
    OP
       20 天前
    没问题的,原来是 C 用 __attribute__((packed)) 消除了内存对齐
    patrickyoung
        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
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3276 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 10:49 · PVG 18:49 · LAX 03:49 · JFK 06:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.