【发布时间】:2018-03-22 07:06:17
【问题描述】:
谁能帮我这行代码是做什么的:
((struct mac_tb_ind *) (tb_p->data))->first_bit = 0;
结构体定义如下:(tb_p 是一种 mem_block_t)
struct mac_tb_ind {
unsigned char *data_ptr;
unsigned short size;
unsigned char error_indication;
unsigned char first_bit;
};
typedef struct mem_block_t {
struct mem_block_t *next;
struct mem_block_t *previous;
unsigned char pool_id;
unsigned char *data;
} mem_block_t;
【问题讨论】:
-
成员tb_p->data其实是一个struct mac_tb_ind指针,设置this的first_bit字段为0。
-
哇,这是一些糟糕的代码。
-
@AnttiHaapala 但是为什么呢?我不能仅从有效的演员阵容中得出结论。
-
该行代码用于调用未定义的行为错误。 stackoverflow.com/questions/98650/…
-
@Lundin 我认为
unsigned char*在这里用作void*。除非data的提供者输入错误的指针,否则这里没有UB。即使data指针错误,UB 也应该是由对齐问题引起的,而不是严格的别名。老实说,我发现您的链接中接受的答案非常具有误导性。请参阅this 帖子,其中一位回答者和我被它误导了。原来-strict-aliasing标志在谈论完全不同的事情。