gpt4 book ai didi

c - 为什么当我修改函数内部的链表时会出现段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:24 25 4
gpt4 key购买 nike

<分区>

我编写了一个辅助函数,旨在将节点添加到链表中。这是要通过以下部分完成的位置。

tcb_add(running,temp);

该段应该为运行增加温度。这是我的函数代码。

void tcb_add(tcb_t *first, tcb_t *second) { //fix
if (first == NULL) {
first = second;
} else {
while (first->next != NULL) {
first = first->next;
}
first->next = second;
}
}

如果不使用行 tcb_add(running,temp); 我只是说 running = temp 它有效。但对于我代码的其他部分,我希望拥有该功能,因为它只是那么简单,因为 temp 是第一个添加的节点。任何帮助将不胜感激。

当然还有更多代码,但我相信我已将问题隔离开来。

这是我调用 tcb_add(running,temp); 时 Valgrind 的输出,但请记住,当我改为说 running = temp 时,该段有效。

==30628== Memcheck, a memory error detector
==30628== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==30628== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==30628== Command: ./test00
==30628==
==30628== Invalid read of size 8
==30628== at 0x400930: t_create (t_lib.c:42)
==30628== by 0x4007F9: main (test00.c:25)
==30628== Address 0x8 is not stack'd, malloc'd or (recently) free'd
==30628==
==30628==
==30628== Process terminating with default action of signal 11 (SIGSEGV)
==30628== Access not within mapped region at address 0x8
==30628== at 0x400930: t_create (t_lib.c:42)
==30628== by 0x4007F9: main (test00.c:25)
==30628== If you believe this happened as a result of a stack
==30628== overflow in your program's main thread (unlikely but
==30628== possible), you can try to increase the size of the
==30628== main thread stack using the --main-stacksize= flag.
==30628== The main thread stack size used in this run was 8388608.
==30628==
==30628== HEAP SUMMARY:
==30628== in use at exit: 67,432 bytes in 4 blocks
==30628== total heap usage: 4 allocs, 0 frees, 67,432 bytes allocated
==30628==
==30628== LEAK SUMMARY:
==30628== definitely lost: 24 bytes in 1 blocks
==30628== indirectly lost: 936 bytes in 1 blocks
==30628== possibly lost: 0 bytes in 0 blocks
==30628== still reachable: 66,472 bytes in 2 blocks
==30628== suppressed: 0 bytes in 0 blocks
==30628== Rerun with --leak-check=full to see details of leaked memory
==30628==
==30628== For counts of detected and suppressed errors, rerun with: -v
==30628== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)

编辑

变量running定义如下:

tcb_t *running;
tcb_t *ready;

这是根据结构:

struct tcb_t {
int thread_id;
int thread_priority;
ucontext_t *thread_context;
struct tcb_t *next;
}; typedef struct tcb_t tcb_t;

正如所问的,这是我的 t_create 函数:

int t_create(void (*fct)(int), int id, int pri) {
size_t sz = 0x10000;
ucontext_t *uc;
uc = (ucontext_t *)malloc(sizeof(ucontext_t));

getcontext(uc);
uc->uc_stack.ss_sp = malloc(sz); /* new statement */
uc->uc_stack.ss_size = sz;
uc->uc_stack.ss_flags = 0;
uc->uc_link = running->thread_context;

makecontext(uc, (void (*)(void))fct, 1, id);
tcb_t *new_tcb = malloc(sizeof(tcb_t));
new_tcb->thread_context = uc;
new_tcb->thread_id = id;
new_tcb->thread_priority = pri;
new_tcb->next = NULL;
tcb_t *tmp = ready;
if (tmp == NULL) // I would like to replace this portion with tcb_add()
ready = new_tcb;
else {
while (tmp->next != NULL) {
tmp = tmp->next;
}
tmp->next = new_tcb;// To here
}

还要进一步解释,虽然 valgrind 报告提到 t_create 这是我认为问题发生的地方。

void t_init() {
tcb_t *temp = tcb_create();
temp->thread_context = (ucontext_t *) malloc(sizeof(ucontext_t));
temp->thread_id = 0;
temp->thread_priority = 0;
temp->next = NULL;

getcontext(temp->thread_context);

tcb_add(running,temp); //DOES NOT WORK
//running = temp; //WORKS
ready = NULL;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com