我用C语言编写了一个函数,该函数从链接列表中弹出第一个节点,并进行相应的更改,因此列表从下一个节点开始。但是,这会导致细分错误,我不确定为什么。
我尝试阅读Valgrind文档并重新分析功能,但我不确定是哪里出了问题。
Valgrind反复提到的一个错误是:
==366== Use of uninitialised value of size 8
==366== at 0x401021: tcb_remove (t_lib.c:149)
==366== by 0x400D70: t_terminate (t_lib.c:75)
==366== by 0x40079C: single_thread (test01x.c:26)
==366== by 0x4E845CF: ??? (in /lib/x86_64-linux-gnu/libc-2.23.so)
==366== by 0x525605F: ???
但是,这似乎不是分段错误的原因。
下一个错误发生了一次,但似乎是导致错误的原因。
==366== Invalid read of size 8
==366== at 0x401012: tcb_remove (t_lib.c:148)
==366== by 0x400D8A: t_terminate (t_lib.c:78)
==366== by 0x40079C: single_thread (test01x.c:26)
==366== by 0x4E845CF: ??? (in /lib/x86_64-linux-gnu/libc-2.23.so)
==366== by 0x525605F: ???
==366== Address 0x10 is not stack'd, malloc'd or (recently) free'd
我希望能同时解决这两个问题,但至少无效读会很棒!
下面是Valgrind提到的所有功能:
void t_terminate(void) {
tcb_t *temp = tcb_create(); //Is this necessary?
temp = tcb_remove(&running);
free(temp);
tcb_add(&running,tcb_remove(&ready));
setcontext(running->thread_context);
}
tcb_t* tcb_remove(tcb_t **rem) {
tcb_t *temp = tcb_create();
if (rem != NULL) {
temp = (*rem);
(*rem) = (*rem)->next;
temp->next = NULL;
}
return temp;
}
running
是一个初始化如下的节点:
tcb_t *running;
具有以下结构:
struct tcb_t {
int thread_id;
int thread_priority;
ucontext_t *thread_context;
struct tcb_t *next;
}; typedef struct tcb_t tcb_t;
还有更多代码,因此,如果您需要任何其他信息,请告诉我。分享我没有问题,但我只是觉得那会让人不知所措。
这是函数tcb_create()
和tcb_add()
,因为它们在上面被调用。
tcb_t* tcb_create() { //works
tcb_t *temp = malloc(sizeof(tcb_t));
return temp;
}
void tcb_add(tcb_t **first, tcb_t *second) { //works
if (*first == NULL) {
*first = second;
} else {
while ((*first)->next != NULL) {
*first = (*first)->next;
}
(*first)->next = second;
}
}