<分区>
我遇到了一段代码,对我来说应该因段错误而崩溃,但它运行顺利。有问题的代码加上相关的数据结构如下(上面有相关注释):
typedef struct {
double length;
unsigned char nPlaced;
unsigned char path[0];
}
RouteDefinition* Alloc_RouteDefinition()
{
// NB: The +nBags*sizeof.. trick "expands" the path[0] array in RouteDefinition
// to the path[nBags] array
RouteDefinition *def = NULL;
return (RouteDefinition*) malloc(sizeof(RouteDefinition) + nBags * sizeof(def->path[0]));
}
为什么会这样?我收集到 sizeof char* 将解析为给定体系结构上指针的大小,但它不应该在取消引用 NULL 时崩溃和燃烧
-指针?