【问题标题】:double free or corruption (!prev) in c , using thread, mallocc 中的双重释放或损坏(!prev),使用线程,malloc
【发布时间】:2016-11-17 14:15:07
【问题描述】:

我厌倦了这个问题。我也使用 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;
} 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;
}

【问题讨论】:

  • 如果你真的需要这方面的帮助。给我们一些更多背景信息,输入一个连贯的问题,然后告诉我们你的代码试图完成什么。

标签: c pthreads malloc free


【解决方案1】:

您将data3[j] 分配给tdata[j].data

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

因此将它们都传递给free() 将导致您所说的双重释放错误。

如果您只想复制指针并且不需要复制 data3[j] 中的值,请删除该部分

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

因为以后会覆盖变量tdata[i].data,导致内存泄漏。同时删除部分

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

因为它会导致上面描述的双释放错误。

【讨论】:

  • 非常感谢。我删除 for(i=tcount-1;i>=0;i--){ free(tdata[i].data); } 现在,不再有双重释放...错误。我的目标是使用冒泡排序的多线程进行 int data[100] 排序。稍后使用合并排序线程的排序数组。
猜你喜欢
  • 1970-01-01
  • 2020-12-30
  • 2022-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多