data.h
data.h
struct Sub {
int n;
struct Sub *next;
}
struct Super {
struct Sub *Sub
void (*addSub)(struct Super *self, struct Sub *subRef);
}
data.c
data.c
static void addSub(struct Super *self, struct Sub *subRef) {
struct Sub *head = self->Sub;
while(head != NULL) { // throwing segmentation fault somewhere here
head = head->next;
}
// assign subRef once we reach to the end.
}
struct Super *newSuper() {
struct Super *super = malloc(sizeof(struct Super));
super->addSub = addSub;
return super;
}
data_test.c
data_test.c
int main() {
struct Super *super = newSuper();
super->addSub(super, malloc(sizeof(struct Sub)));
return 0;
}
I'm relatively new to C, implemented linked list long time back but can't seem to get my head around the null problem, that's how it used to be. Please help me to progress in detecting the end of the list and adding the new value to the end. Thanks.
我对C比较陌生,实现链表很长一段时间了,但是我似乎想不出零问题,这就是它过去的样子。请帮助我检测列表的末尾,并在末尾添加新的值。谢谢。
2 个解决方案
#1
0
you have to initialize you new object
你必须初始化你的新对象
struct Super *newSuper() {
struct Super *super = malloc(sizeof(struct Super));
super->addSub = addSub;
super->Sub = NULL;
return super;
}
malloc'ed memory is not cleared to any value; it contains junk.
malloc'ed内存未清除到任何值;它包含了垃圾。
you should also have a newSub function to create instance of your Sub objects instead of the caller using raw malloc
您还应该有一个newSub函数来创建子对象的实例,而不是使用原始malloc的调用者
#2
0
Your addSub
function is not doing the real thing, means it's not adding anything. you are traversing to the null
node of the list and loosing the parent node. Better check for head->next != null
and then add sub
to the end as shown below. (I assume that you want to add subRef
to the end of the list)
你的addSub函数没有做真正的事情,意味着它没有添加任何东西。您正在遍历列表的空节点并释放父节点。接下来最好检查head-> != null,然后在末尾添加sub,如下所示。(我假设您想在列表的末尾添加subRef)
static void addSub(struct Super *self, struct Sub *subRef) {
struct Sub *head = self->Sub;
if(head == NULL)
{
self->Sub = sub;
return;
}
while(head->next != NULL) { // throwing segmentation fault somewhere here
head = head->next;
head->next = subRef;
subRef->next = NULL; //This step is only required if subRef->next
//was not set to null before the call
}
#1
0
you have to initialize you new object
你必须初始化你的新对象
struct Super *newSuper() {
struct Super *super = malloc(sizeof(struct Super));
super->addSub = addSub;
super->Sub = NULL;
return super;
}
malloc'ed memory is not cleared to any value; it contains junk.
malloc'ed内存未清除到任何值;它包含了垃圾。
you should also have a newSub function to create instance of your Sub objects instead of the caller using raw malloc
您还应该有一个newSub函数来创建子对象的实例,而不是使用原始malloc的调用者
#2
0
Your addSub
function is not doing the real thing, means it's not adding anything. you are traversing to the null
node of the list and loosing the parent node. Better check for head->next != null
and then add sub
to the end as shown below. (I assume that you want to add subRef
to the end of the list)
你的addSub函数没有做真正的事情,意味着它没有添加任何东西。您正在遍历列表的空节点并释放父节点。接下来最好检查head-> != null,然后在末尾添加sub,如下所示。(我假设您想在列表的末尾添加subRef)
static void addSub(struct Super *self, struct Sub *subRef) {
struct Sub *head = self->Sub;
if(head == NULL)
{
self->Sub = sub;
return;
}
while(head->next != NULL) { // throwing segmentation fault somewhere here
head = head->next;
head->next = subRef;
subRef->next = NULL; //This step is only required if subRef->next
//was not set to null before the call
}