c - where is the pthread segfault happening? -


in program, provide directory contains text files. each of text files contain few hundred lines in following format

username,password,bloodtype,domain,number 

i create thread each file in directory merge-sort(by number) these lines array char* text_lines[6000];

i can't figure out why i'm getting segmentation fault because i'm getting different output on every run.

heres code:

#include <string.h> #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <sys/types.h> #include <dirent.h> #include <string.h>  void store_line(char* line); void* my_merge_sort(void* file);  char** text_lines;  int main(int argc, char* argv[]) {      if(argc != 2)     {         fprintf(stderr, "usage: ./coolsort <directory>\n");     }     else     {         text_lines = malloc(6000 * sizeof(char*));         dir* the_directory;         int filecount = 0;         struct dirent* directory_files[50];         if((the_directory = opendir(argv[1])) != null)         {             //make list of files in directory             while((directory_files[filecount++] = readdir(the_directory))) ;             filecount--;              //<<<debugging info>             int i;             fprintf(stderr,"there %i files in %s:\n", filecount, argv[1]);             for(i = 0; < filecount; i++)             {                 fprintf(stderr, "%s\n",directory_files[i]->d_name);             }             char cwd[512];             chdir(argv[1]);             getcwd(cwd, sizeof(cwd));             fprintf(stderr, "the cwd is:  %s\n", cwd);             //<debugging info>>>              //lets start threads             pthread_t threads[filecount-2];             int x = 0;             for(i = 0; < (filecount); i++ )             {                 if (!strcmp (directory_files[i]->d_name, "."))                     continue;                 if (!strcmp (directory_files[i]->d_name, ".."))                         continue;                 pthread_create(&threads[x++], null, my_merge_sort, (void*)directory_files[i]->d_name);             }             //do stuff here              //         }         else         {             fprintf(stderr, "failed open directory: %s\n", argv[1]);         }     } }  void* my_merge_sort(void* file) {     fprintf(stderr, "we got function!\n");     file* fp = fopen(file, "r");     char* buffer;     char* line;     char delim[2] = "\n";     int numbytes;      //minimize i/o's reading entire file memory;     fseek(fp, 0l, seek_end);     numbytes = ftell(fp);     fseek(fp, 0l, seek_set);     buffer = (char*)calloc(numbytes, sizeof(char));     fread(buffer, sizeof(char), numbytes, fp);     fclose(fp);       //now read buffer '\n' delimiters     line = strtok(buffer, delim);     fprintf(stderr, "heres while loop\n");     while(line != null)     {         store_line(line);         line = strtok(buffer, null);     }     free(buffer); }  void store_line(char* line) {        //extract id.no, fifth comma-seperated-token.      char delim[] = ",";     char* buff;     int id;     int i;     strtok(line, delim);     for(i = 0; < 3; i++)     {         strtok(line, null);     }     buff = strtok(line, null);     id = atoi(buff);      //copy line text_lines[id]     memcpy(text_lines[id], line, strlen(line)); } 

edit: checked make sure fit initial array, , found highest id 3000;

  1. you use of strtok() wrong:

    line = strtok(buffer, null); 

    should

    line = strtok(null, delim); 

    another mistakes should fixed similarly.

  2. the elements of text_lines uninitialized:

    text_lines = malloc(6000 * sizeof(char*)); 

    this allocated 6000 pointers char, none of these pointers initialized.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -