C中的pthread_连接分段错误

C中的pthread_连接分段错误,c,multithreading,pthreads,pthread-join,C,Multithreading,Pthreads,Pthread Join,我的程序的目的是打开一个目录,为其中的每个文件创建一个线程,并将它的结构放入一个结构数组(文件)。但是,即使我将这些函数更改为仅{returnnull;},我仍然会遇到一个分段错误,即调用pthread_join。有人能帮忙吗 int return_code; DIR *dir; struct dirent entry; struct dirent *result; //6 = # of files within data file_values* files = malloc(MAX_FILE

我的程序的目的是打开一个目录,为其中的每个文件创建一个线程,并将它的结构放入一个结构数组(文件)。但是,即使我将这些函数更改为仅{returnnull;},我仍然会遇到一个分段错误,即调用pthread_join。有人能帮忙吗

int return_code;
DIR *dir;
struct dirent entry;
struct dirent *result;
//6 = # of files within data
file_values* files = malloc(MAX_FILE_VALUE*sizeof(file_values));
if ((dir = opendir("data")) == NULL){
    perror("opendir() error");
}
else {
    int numberOfFiles = 0;
    pthread_t thread_id[MAX_FILE_VALUE];  //keep track of the thread ids

    clock_t start, stop;
    start = clock();

    //read from directory
    for (return_code = readdir_r(dir, &entry, &result); result != NULL && return_code == 0; return_code = readdir_r(dir, &entry, &result)){

        //write the nessecary information from the read file to the struct
        files[numberOfFiles].filename = malloc(50);
        strcpy(files[numberOfFiles].filename, entry.d_name);
        files[numberOfFiles].fileCount = numberOfFiles;

        //exclude . and ..
        if(strcmp(files[numberOfFiles].filename, ".") != 0 && strcmp(files[numberOfFiles].filename, "..") != 0){

            //generate a thread
            pthread_create (&thread_id[numberOfFiles], NULL , &map, &files[numberOfFiles]);
            //map the information from one file into a struct

            //Keeps track of total number of files within a directory
            numberOfFiles++;
        }
    }

    //join all of the threads when it's done
    int i;
    for(i = 0 ; i < MAX_FILE_VALUE ; i++){
        pthread_join(thread_id[i],NULL);
    }
int返回\ u码;
DIR*DIR;
结构直接入口;
结构方向*结果;
//6=#数据中的文件
文件值*files=malloc(最大文件值*sizeof(文件值));
if((dir=opendir(“数据”))==NULL){
perror(“opendir()错误”);
}
否则{
int numberOfFiles=0;
pthread_t thread_id[MAX_FILE_VALUE];//跟踪线程id
时钟没有启动,停止;
开始=时钟();
//从目录中读取
for(return\u code=readdir\r(dir,&entry,&result);result!=NULL&&return\u code==0;return\u code=readdir\r(dir,&entry,&result)){
//将读取文件中的nessecary信息写入结构
文件[numberOfFiles].filename=malloc(50);
strcpy(files[numberOfFiles].filename,entry.d_name);
文件[numberOfFiles].fileCount=numberOfFiles;
//排除。和。。
如果(strcmp(文件[numberOfFiles].filename,“.”!=0&&strcmp(文件[numberOfFiles].filename,“…”)!=0){
//生成线程
pthread_create(&thread_id[numberOfFiles],NULL,&map,&files[numberOfFiles]);
//将信息从一个文件映射到一个结构
//跟踪目录中的文件总数
numberOfFiles++;
}
}
//完成后连接所有线程
int i;
对于(i=0;i
如果
numberofiles
,您的最后一个循环将尝试加入无效/未初始化的
pthread\u id
值,这肯定会导致系统调用中出现segfault

最后一个循环应该是:

for(i = 0 ; i < numberOfFiles ; i++){
        pthread_join(thread_id[i],NULL);
    }
for(i=0;i
如果
numberofiles
,您的最后一个循环将尝试加入无效/未初始化的
pthread\u id
值,这肯定会导致系统调用中出现segfault

最后一个循环应该是:

for(i = 0 ; i < numberOfFiles ; i++){
        pthread_join(thread_id[i],NULL);
    }
for(i=0;i