当我尝试编译下面的代码时,gcc -O3 -Wall -Werror -std=c99 main.c
我在#3 中收到类似“取消引用类型双关指针将破坏严格别名规则”的错误,但在#2 或#1 中没有。我可以取消引用类型双关的“char *”,但为什么我不能对灵活数组做同样的事情呢?
#include <stdlib.h>
#include <stdio.h>
struct Base {
void (*release) (struct Base *);
size_t sz;
char *str_foo;
char rest[];
};
struct Concrete {
char *str_bar;
};
void
Base_release(struct Base *base)
{
free(base);
}
struct Base *
Base_new(size_t sz_extra)
{
size_t sz = sizeof(struct Base) + sz_extra;
struct Base *base = (struct Base *)malloc(sz);
base->release = &Base_release;
base->sz = sz;
base->str_foo = "foo";
return base;
}
#define BASE_FREE(_obj) (_obj)->release(_obj)
#define BASE_CAST(_type, _obj) ((struct _type *)((_obj)->rest))
#define BASE_CAST_2(_type, _obj) ((struct _type *)((char *)(_obj)+sizeof(struct Base)))
struct Base *
Concrete_new()
{
struct Base *base = Base_new(sizeof(struct Concrete));
struct Concrete *concrete = BASE_CAST(Concrete, base);
concrete->str_bar = "bar";
return base;
}
int main(int argc, const char *argv[])
{
struct Base *instance = Concrete_new();
printf("Base str: %s\n", instance->str_foo);
// #1 - Legal
struct Concrete *cinstance = BASE_CAST(Concrete, instance);
printf("#1: Concrete str: %s\n", cinstance->str_bar);
// #2 - Legal
printf("#2: Concrete str: %s\n", BASE_CAST_2(Concrete, instance)->str_bar);
// #3 - Compile error
printf("#3: Concrete str: %s\n", BASE_CAST(Concrete, instance)->str_bar);
BASE_FREE(instance);
return 0;
}
编辑1: 下面有更具体的例子显示问题:
struct s {
char a;
};
char *const a = malloc(sizeof(struct s));
char b[sizeof(struct s)];
((struct s *)((char *)a))->a = 5; // This is a valid case
((struct s *)(a))->a = 5; // OK
((struct s *)((char *)b))->a = 5; // ???