我正在调试以下代码片段来分析结构成员的字节对齐。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
struct v_hdr {
uint16_t vlan_tci;
uint16_t eth_proto;
}__attribute__((aligned(1)));
struct esp_hdr {
uint32_t fpa_esp_spi;
uint32_t fpa_esp_sequenceid;
}__attribute__((aligned(1)));
struct f_cookie {
uint16_t vlan_ports;
uint16_t untagged_vlan_ports;
struct v_hdr vlan_hdr;
uint16_t tcp_flags;
uint32_t *eth_hdr;
uint32_t *ipv4_hdr;
struct esp_hdr esp;
}__attribute__((aligned(1)));
struct metadata {
struct f_cookie flow_cookie;
} __attribute__((aligned(1)));
int main ()
{
struct metadata meta;
memset (&meta, 0, sizeof(struct metadata));
/* 16 */ meta.flow_cookie.vlan_ports = 0xffff;
/* 16 */ meta.flow_cookie.untagged_vlan_ports = 0xffff;
/* 16 */ meta.flow_cookie.vlan_hdr.vlan_tci = 0xffff;
/* 16 */ meta.flow_cookie.vlan_hdr.eth_proto = 0xffff;
/* 16 */ meta.flow_cookie.tcp_flags = 0xffff;
/* 32 */ meta.flow_cookie.eth_hdr = 0xffffffff;
/* 32 */ meta.flow_cookie.ipv4_hdr = 0xffffffff;
/* 32 */ meta.flow_cookie.esp.fpa_esp_spi = 0xffffffff;
/* 32 */ meta.flow_cookie.esp.fpa_esp_sequenceid = 0xffffffff;
return 0;
}
这是 return 语句之前的 gdb x 转储 -
(gdb) p sizeof meta
$2 = 40
(gdb) x/40bt &meta
0x7fffffffe2d0: 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
0x7fffffffe2d8: 11111111 11111111 00000000 00000000 00000000 00000000 00000000 00000000
0x7fffffffe2e0: 11111111 11111111 11111111 11111111 00000000 00000000 00000000 00000000
0x7fffffffe2e8: 11111111 11111111 11111111 11111111 00000000 00000000 00000000 00000000
0x7fffffffe2f0: 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
我的问题是为什么 meta.flow_cookie.eth_hdr
没有放置在 tcp_flags
之后的 4 字节边界处?即在 (0x7fffffffe2d8 + 4)
处。并且 meta.flow_cookie.ipv4_hdr
不在 0x7fffffffe2e0
处?
尽管应用了 aligned(1)
属性,为什么这两个成员都放置在 8 字节边界?附言。我不想使用属性packed
。我只希望成员尽可能与 4 字节地址对齐。可以实现吗?
最佳答案
引用您的代码摘录:
/* 16 */ meta.flow_cookie.tcp_flags = 0xffff; /* 32 */ meta.flow_cookie.eth_hdr = 0xffffffff; /* 32 */ meta.flow_cookie.ipv4_hdr = 0xffffffff;
您假设 meta.flow_cookie.eth_hdr
和 meta.flow_cookie.ipv4_hdr
指针 是 32 位长,并且您的系统,根据您的 gdb
转储,似乎不是 32 位架构。
请记住,sizeof(uint32_t *)
与 sizeof(uint32_t)
不同。如果由于某种原因这些值相同(在 32 位架构的情况下),则纯粹是巧合。
指针的大小始终足以容纳属于底层体系结构的完整地址空间的任何地址。例如,64 位架构上的 sizeof(uint32_t *)
至少为 8 个字节长(64 位长)。
由于您的体系结构似乎具有 64 位地址空间,因此您对对齐所做的假设是有缺陷的。
关于c - 使用属性对齐时的内存对齐(1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38260103/