sách gpt4 ai đã đi

c - pthread_join 自由() : invalid pointer error

In lại 作者:行者123 更新时间:2023-11-30 15:42:52 29 4
mua khóa gpt4 Nike

我有这样一段代码:

void *write_parallel(void *num_for_chunk) {

struct rusage *sum = (struct rusage*) malloc(sizeof(struct rusage));
if(sum==NULL) {
perror("Writing buffer: ");
}

sum->ru_utime.tv_sec = 0;
sum->ru_utime.tv_usec = 0;
sum->ru_stime.tv_sec = 0;
sum->ru_stime.tv_usec = 0;

pthread_exit(sum);
}

我有一个线程表:

pthread_t* thread_table = (pthread_t *) malloc(sizeof(pthread_t)*num_of_threads);

我正在创建一个线程:

pthread_create(thread_table+(sizeof(pthread_t)*j),NULL,write_parallel,&num_for_chunk);

并加入:

pthread_join(*(thread_table+(sizeof(pthread_t)*j)),&part_res);

当我运行程序时,我得到:

*** Error in `./write_test': free(): invalid pointer: 0x00007f6b6e44a700 ***

我认为线程消失时存在问题,我想获取传递给 pthread_exit 的值,但我不知道如何修复它。

1 Câu trả lời

假设 thread_table Được tuyên bố là pthread_t 数组:

pthread_t thread_table[];

hoặc

pthread_t * thread_table = calloc(, sizeof(*thread_table);

只需在访问其元素时删除 sizeof(pthread_t) 即可。

Vì thế

pthread_create(thread_table+(sizeof(pthread_t)*j), ...

会变成

pthread_create(thread_table+j, ...

还有

pthread_join(*(thread_table+(sizeof(pthread_t)*j)), ...

会变成

pthread_join(*(thread_table + j), ...

lý lịch:将(非 trống rỗng *)指针 T p 增加 1 将指针移动到指向到 T 类型的下一个元素。这意味着指针会增加 sizeof(T) 或相等的 sizeof(*p) 字节。

<小时>

访问数组元素的更简单、更直接的方法是使用 [] 运算符,如下所示:

pthread_create(&thread_table[j], ...

...

pthread_join(thread_table[j], ...

关于c - pthread_join 自由() : invalid pointer error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20024334/

29 4 0
行者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