在c中使用线程malloc进行双重释放或损坏(!prev)
我厌倦了这个问题。我也使用valgrind。但我不知道为什么。请找出我的代码中的问题在c中使用线程malloc进行双重释放或损坏(!prev),c,pthreads,malloc,free,C,Pthreads,Malloc,Free,我厌倦了这个问题。我也使用valgrind。但我不知道为什么。请找出我的代码中的问题 #include <stdio.h> #include <stdlib.h> #include <pthread.h> static pthread_t *tid=NULL; static int **data3=NULL; typedef struct _Thdata { int *data; int size; int nthread; } Th
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static pthread_t *tid=NULL;
static int **data3=NULL;
typedef struct _Thdata
{
int *data;
int size;
int nthread;
} Thdata;
Thdata *tdata=NULL;
void *bubble(void *d){
Thdata *arr =(Thdata *)d;
int i,j,tmp;
int n=arr->size;
printf("thread #=%d n=%d\n",arr->nthread,n);
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if((arr->data[j])>(arr->data[j+1]))
{
tmp = (arr->data[j]);
(arr->data[j])=(arr->data[j+1]);
(arr->data[j+1])=tmp;
}
}
}
for(j=0;j<n;j++)
printf("%d ",(arr->data[j]));
printf("\n");
pthread_exit((void *)1);
}
int main(int argc, char **argv){
FILE * fd;
int i,j;
int data[100];
int tcount = atoi(argv[1]);
int n = 100/tcount;
int err;
void *b;
//dynamic data
tid = (pthread_t *)malloc(tcount* sizeof(pthread_t));
data3 = (int **)malloc(tcount *sizeof(int*));
for( i=0; i<tcount; i++)
data3[i] = (int *)malloc((100/tcount) *sizeof(int));
tdata = (Thdata *)malloc(tcount*sizeof(Thdata));
for(i=0;i<tcount; i++) {
tdata[i].data =(int *)malloc(n*sizeof(int));
}
//dynamic data end
fd = fopen("data.txt", "r");
printf("tcount = %d n=%d\n",tcount,n);
// origin data
for(i =0; i<100;i++)
{
fscanf(fd, "%d",&data[i]);
printf("%d ", data[i]);
}
printf("\n");
for(j=0;j<tcount;j++){
for(i=0;i<n;i++){
data3[j][i]=data[n*j+i];
printf("%d ",data3[j][i]);
//tdata[j].data[i]=data[j][i];
}
printf("\n");
tdata[j].data=data3[j];
tdata[j].size=n;
tdata[j].nthread=0;
}
for(j=0;j<tcount;j++){
for(i=0;i<n;i++){
printf("%d ",tdata[j].data[i]);
}
printf("tdata[%d].size = %d",j,tdata[j].size);
printf("\n");
}
for(i =0; i<tcount;i++)
{
err=pthread_create(&tid[i],NULL,bubble,(void *)&tdata[i]);
if(err != 0)
printf("creat thread error");
tdata[i].nthread=i;
}
for(i=0;i<tcount;i++)
pthread_join(tid[i],&b);
for(i=tcount-1;i>=0;i--){
free(tdata[i].data);
}
free(tdata);
for(int i=tcount-1; i>=0; i--)
free(data3[i]);
free(data3);
free(tid);
fclose(fd);
return 0;
}
#包括
#包括
#包括
静态pthread_t*tid=NULL;
静态int**data3=NULL;
typedef结构数据
{
int*数据;
整数大小;
int-nthread;
}Thdata;
Thdata*tdata=NULL;
void*bubble(void*d){
Thdata*arr=(Thdata*)d;
int i,j,tmp;
int n=arr->size;
printf(“线程#=%d n=%d\n”,arr->nthread,n);
对于(i=0;i(arr->data[j+1]))
{
tmp=(arr->data[j]);
(arr->data[j])=(arr->data[j+1]);
(arr->data[j+1])=tmp;
}
}
}
对于(j=0;jdata[j]);
printf(“\n”);
pthread_exit((void*)1);
}
int main(int argc,字符**argv){
文件*fd;
int i,j;
int数据[100];
int tcount=atoi(argv[1]);
int n=100/t计数;
INTERR;
无效*b;
//动态数据
tid=(pthread_t*)malloc(tcount*sizeof(pthread_t));
数据3=(int**)malloc(tcount*sizeof(int*);
对于(i=0;i您将data3[j]
分配给tdata[j]。数据作为
tdata[j].data=data3[j];
因此,将它们传递给free()
将导致您所说的双重自由错误
如果只想复制指针,而不需要复制data3[j]
中的值,请删除该零件
for(i=0;i<tcount; i++) {
tdata[i].data =(int *)malloc(n*sizeof(int));
}
for(i=tcount-1;i>=0;i--){
free(tdata[i].data);
}
因为它会导致上面描述的双自由错误。如果您真的需要帮助,请提供更多的背景信息,键入一个连贯的问题,并告诉我们您的代码试图实现什么。非常感谢。我删除(i=tcount-1;i>=0;i--){free(tdata[i].data);}现在,不再有双自由…错误。我的目标是int data[100]使用气泡排序的多线程排序。稍后使用合并排序线程的排序数组。