pthreads - double free or corruption (!prev) in c , using thread, malloc -


i'm tired problem. use valgrind also. don't know why. please find problem in code.

#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; } 

you assigned data3[j] tdata[j].data as

tdata[j].data=data3[j]; 

so passing both of them free() cause double-free error said.

if want copy pointers , copying values in data3[j] isn't needed, remove part

for(i=0;i<tcount; i++) {     tdata[i].data =(int *)malloc(n*sizeof(int)); } 

because variable tdata[i].data overwritten later , memory leak caused. remove part

for(i=tcount-1;i>=0;i--){     free(tdata[i].data); } 

because cause double-free error descrived above.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -