sách gpt4 ai đã đi

c - 链表的 pthread 段错误 - 初学者

In lại 作者:行者123 更新时间:2023-12-04 05:54:57 hai mươi bốn 4
mua khóa gpt4 Nike

这是我第一次使用 pthread。我遇到了麻烦,因为有时我的程序段错误,有时没有。我的程序中有一些函数可以执行一些基本任务(用 C 编写),例如创建链表、向列表添加项目以及从列表中删除项目。每个函数都会创建它自己的列表副本,因此我认为它们不会相互交互,因此不需要互斥锁。无论如何,下面是我的代码,如果有人有任何想法或者是否有任何“常见”的初学者 pthread 错误。

我并行运行每个函数 1000 次,有时是段错误,有时不是。我注意到它只发生在这 3 个功能的组合中。

这个过程是这样的:
- 创建线程
- 并行运行线程
- 每个线程调用虚拟函数以执行给定次数的任务
- 该虚拟函数还调用其他函数

我认为这可能与内存使用/分配有关,因为所有这些功能都与创建/删除链表节点有关。非常感谢。

以下是创建和连接:

 pthread_create(&t7, NULL, (void*)&p4, (void*)var);
pthread_create(&t8, NULL, (void*)&p5a, (void*)var);
pthread_create(&t9, NULL, (void*)&p5b, (void*)var);
pthread_join(t7, NULL);
pthread_join(t8, NULL);
pthread_join(t9, NULL);

以下是虚拟函数:
void p4(int *nptr){
int n = *nptr;
// Get current time
struct timeval t0, t1;
gettimeofday(&t0,0);

int i = 0;
LIST *list = (LIST*)malloc(sizeof(LIST));
for(i=0;i<>
f4(list);
deleteList(list);
}
// Get current time and find time elapsed
gettimeofday(&t1,0);
float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
printf("Successful execution of p4 in %f microseconds.\n", elapsed);
free(list);
}
void p5a(int *nptr){
int n = *nptr;
LIST *list = (LIST*)malloc(sizeof(LIST));
f4(list);
// Get current time
struct timeval t0, t1;
gettimeofday(&t0,0);

int i = 0;
for(i=0;i<>
f5a(list);
}
// Get current time and find time elapsed
gettimeofday(&t1,0);
float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
printf("Successful execution of p5a in %f microseconds.\n", elapsed);
}
void p5b(int *nptr){
int n = *nptr;
LIST *list = (LIST*)malloc(sizeof(LIST));
f4(list);
int i = 0;
for(i=0;i<>
f5a(list);
}
// Get current time
struct timeval t0, t1;
gettimeofday(&t0,0);
for(i=0;i<>
f5b(list);
}
// Get current time and find time elapsed
gettimeofday(&t1,0);
float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
printf("Successful execution of p5b in %f microseconds.\n", elapsed);
}

以下是用于执行常规任务的函数:
// FUNCTION: initialize a linked list with pointers and insert a last element
void f4(LIST *L1){
// initialize an empty linked list if L1 = null
if(L1->head == NULL){
NODE *n = (NODE *)malloc(sizeof(NODE));
L1->head = n;
L1->tail = n;
L1->tail->next = NULL;
n->data = 1;
}
// traverse the linked list to the end
NODE *iter = L1->head;
while(iter->next != NULL)
iter = iter->next;
// insert a new 2 element
NODE *new = (NODE *)malloc(sizeof(NODE));
new->data = 2; // arbitrary for testing
new->next = NULL;
iter->next = new;
L1->tail = new;
}

// FUNCTION: add an item to the end of a list (queue)
void f5a(LIST *list){
NODE *new = (NODE *)malloc(sizeof(NODE));
new->data = 999;
new->next = NULL;
list->tail->next = new;
list->tail = new;
}

// FUNCTION: remove an item from the beginning of a list (queue)
void f5b(LIST *list){
NODE *remove = list->head;
list->head = list->head->next;
free(remove);
}

1 Câu trả lời

如果您阅读 trung tâm您可以看到手册页没有将分配的内存初始化为 0。因此,您 trung tâm内存在 p4 , p5ap5b职能。在此之后,您调用 f4没有初始化的 LIST 内容的函数。

hiện hữu p4Việc sử dụng của bạn if(L1->head == NULL) 检查有效指针的函数但它可能不为空。所以你不为 L1->head 分配内存并且,在此函数之后,在 f5b您正在释放未分配的指针的函数。

建议:

  • 始终初始化分配的区域。
  • 随时查看 trung tâm返回指针
  • 关于c - 链表的 pthread 段错误 - 初学者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9618076/

    hai mươi bốn 4 0
    Bài viết được đề xuất: jsf-2 - JSF2+AJAX : Keep Scrollbar position, 普通莫哈拉
    Bài viết được đề xuất: java - 在 Swing 中构建类似于 SO 标记的搜索模块 UI
    Bài viết được đề xuất: DBpedia/Yago 集成
    Bài viết được đề xuất: r - 使用 R 分析 PDF 表单中的一组复选框
    行者123
    Hồ sơ cá nhân

    Tôi là một lập trình viên xuất sắc, rất giỏi!

    Nhận phiếu giảm giá Didi Taxi miễn phí
    Mã giảm giá Didi Taxi
    Giấy chứng nhận ICP Bắc Kinh số 000000
    Hợp tác quảng cáo: 1813099741@qq.com 6ren.com