无法将联合成员指定为 NULL
Cannot assign union member as NULL
我有一个联合定义如下:
union simple_list
{
simple_list *next;
int *s;
};
这是我的主要功能:
int main()
{
simple_list *sl;
sl->next = NULL; // core dumped, why?
simple_list sl1;
sl1.next = NULL; // this will be fine
simple_list *sl2;
sl->next = sl2; // this also will be fine
return 0;
}
我不能通过指针访问联合成员之一吗?
新增:
现在,答案很明确了。因为我试图在为它分配内存之前访问一个指针,而这种操作是未定义的。
我这样修改了我的代码,然后一切正常。
simple_list *sl = (simple_list*)malloc(sizeof(union simple_list));
但是,我发现另一个问题:
int main()
{
simple_list *sl = (simple_list*)malloc(sizeof(union simple_list));
sl->next = NULL; // this should be fine and it does
simple_list *sl1;
sl1->next = NULL; // amazing! this also be fine, "fine" means no core dumped
return 0;
}
这是否意味着未定义的操作可能(不是必须)导致核心转储错误?
我用 gcc 4.8.4 编译我的 C 代码。
Ubuntu 14.04 虚拟机。
更新时间:2015-12-16
coredumped 表示段错误。我最近看了一些关于 OS 的书,segmentation fault 意味着你试图访问一些没有为你分配的内存。当我声明一个指针但不为其分配内存时,指针是悬空的。悬垂意味着这个指针有可能指向任何地方,所以根据指向成功与否是合理的。到目前为止一切顺利!
为您的指针分配内存s1
。
s1 = malloc(sizeof(union simple_list));
此外,此代码将无法编译。
您需要为联合内的变量设置联合类型。
union simple_list
{
union simple_list *next;
int *s;
}
您需要分配对象!
simple_list* s1 = malloc(sizeof(simple_list));
s1->next = NULL;
您必须在分配前为 sl
分配内存。否则,sl->next = NULL;
将调用未定义的行为。
simple_list *sl;
声明指向 simple_list
的指针,不分配内存且指针未指向有效的 union
。
sl->next = NULL; // core dumped, why?
因为见上文。此联合实例不存在。
A union
是 C 中的棘手野兽。未定义的行为 永远不会太远。详细:
simple_list *sl; sl->next = NULL;
行为 未定义。您还没有将指针 sl
分配给任何内存。
simple_list sl1; sl1.next = NULL;
没关系。只是不要试图回读 s
成员,因为这样做的行为是 undefined.
simple_list *sl2; sl->next = sl2;
不,这也是 未定义的行为,因为您正在读取未初始化的指针值。
第三点比较微妙。大多数人都知道 dereferencing 一个未初始化的指针是未定义的,但是 reading 在大多数情况下也是未定义的;包括这个。
我有一个联合定义如下:
union simple_list
{
simple_list *next;
int *s;
};
这是我的主要功能:
int main()
{
simple_list *sl;
sl->next = NULL; // core dumped, why?
simple_list sl1;
sl1.next = NULL; // this will be fine
simple_list *sl2;
sl->next = sl2; // this also will be fine
return 0;
}
我不能通过指针访问联合成员之一吗?
新增: 现在,答案很明确了。因为我试图在为它分配内存之前访问一个指针,而这种操作是未定义的。 我这样修改了我的代码,然后一切正常。
simple_list *sl = (simple_list*)malloc(sizeof(union simple_list));
但是,我发现另一个问题:
int main()
{
simple_list *sl = (simple_list*)malloc(sizeof(union simple_list));
sl->next = NULL; // this should be fine and it does
simple_list *sl1;
sl1->next = NULL; // amazing! this also be fine, "fine" means no core dumped
return 0;
}
这是否意味着未定义的操作可能(不是必须)导致核心转储错误?
我用 gcc 4.8.4 编译我的 C 代码。 Ubuntu 14.04 虚拟机。
更新时间:2015-12-16
coredumped 表示段错误。我最近看了一些关于 OS 的书,segmentation fault 意味着你试图访问一些没有为你分配的内存。当我声明一个指针但不为其分配内存时,指针是悬空的。悬垂意味着这个指针有可能指向任何地方,所以根据指向成功与否是合理的。到目前为止一切顺利!
为您的指针分配内存s1
。
s1 = malloc(sizeof(union simple_list));
此外,此代码将无法编译。
您需要为联合内的变量设置联合类型。
union simple_list
{
union simple_list *next;
int *s;
}
您需要分配对象!
simple_list* s1 = malloc(sizeof(simple_list));
s1->next = NULL;
您必须在分配前为 sl
分配内存。否则,sl->next = NULL;
将调用未定义的行为。
simple_list *sl;
声明指向 simple_list
的指针,不分配内存且指针未指向有效的 union
。
sl->next = NULL; // core dumped, why?
因为见上文。此联合实例不存在。
A union
是 C 中的棘手野兽。未定义的行为 永远不会太远。详细:
simple_list *sl; sl->next = NULL;
行为 未定义。您还没有将指针sl
分配给任何内存。simple_list sl1; sl1.next = NULL;
没关系。只是不要试图回读s
成员,因为这样做的行为是 undefined.simple_list *sl2; sl->next = sl2;
不,这也是 未定义的行为,因为您正在读取未初始化的指针值。
第三点比较微妙。大多数人都知道 dereferencing 一个未初始化的指针是未定义的,但是 reading 在大多数情况下也是未定义的;包括这个。