为什么rust libc对struct使用repr(打包)?
Rust libc use为什么rust libc对struct使用repr(打包)?,rust,Rust,Rust libc userepr(packed),如图所示,该结构随后将传递给系统libc。例如,isrepr(packed)然后在中使用 根据提到的文件 那么为什么rust libc使用repr(packeted)而不是repr(C)将结构传递给系统libc呢 那么为什么rust libc使用repr(packeted)而不是repr(C)将结构传递给系统libc呢 一个明显的原因是,等效结构也被指定为在C侧填充。(许多C编译器支持“打包”作为扩展,其含义与Rust中的相同。)Linux上的
repr(packed)
,如图所示,该结构随后将传递给系统libc。例如,isrepr(packed)
然后在中使用
根据提到的文件
那么为什么rust libc使用repr(packeted)
而不是repr(C)
将结构传递给系统libc呢
那么为什么rust libc使用repr(packeted)
而不是repr(C)
将结构传递给系统libc呢
一个明显的原因是,等效结构也被指定为在C侧填充。(许多C编译器支持“打包”作为扩展,其含义与Rust中的相同。)Linux上的epoll_event
的定义证实了这一点:
#ifdef __x86_64__
#define EPOLL_PACKED __attribute__((packed))
#else
#define EPOLL_PACKED
#endif
struct epoll_event {
__u32 events;
__u64 data;
} EPOLL_PACKED;
这同样适用于其他例子
#ifdef __x86_64__
#define EPOLL_PACKED __attribute__((packed))
#else
#define EPOLL_PACKED
#endif
struct epoll_event {
__u32 events;
__u64 data;
} EPOLL_PACKED;