The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
lysShub

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

  •  
  •   lysShub · Aug 14, 2025 · 1951 views
    This topic created in 333 days ago, the information mentioned may be changed or developed.

    在 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 replies    2025-08-14 08:43:55 +08:00
    lysShub
        1
    lysShub  
    OP
       Aug 14, 2025
    没问题的,原来是 C 用 __attribute__((packed)) 消除了内存对齐
    patrickyoung
        2
    patrickyoung  
       Aug 14, 2025 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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3298 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 10:56 · PVG 18:56 · LAX 03:56 · JFK 06:56
    ♥ Do have faith in what you're doing.