由2d数组或strtok引起的分段错误

时间:2018-02-03 12:15:00

标签: c arrays multithreading strtok

我正在尝试创建一个线程并从线程内的stdin读取。在main()中,基于作为用户输入给出的大小,动态地将内存分配给2d数组。在线程中,我从stdin读取并使用strtok将其拆分并将其添加到2d数组中。我不确定为什么会出现分段错误,搜索到SO并且我似乎处理了与strtok相关的所有案例。

这是程序 -

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int dimB;
int dimA;
char** buffer;
char* temp;


void *thread(void *threadid){
    char *buf;//[30] = {};
    size_t len = 0;
    ssize_t read;
    char *line = NULL;
    char *each;

   printf("Hello World!.\n");
   while ((read = getline(&line, &len, stdin)) != -1) {

       printf("%s || \n", line);
       each = strtok(line," ,()");
       printf("************************%s ", each);
       while(each != NULL){
               buf = each;
               strcpy(buffer[0][0], buf);
               printf("%s", buf);
           each = strtok(NULL," ,()");
       }

   }
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
    pthread_t tidMpr;
    long r;
    int i;
    dimB = atoi(argv[1]);
    dimA = atoi(argv[2]);
    pthread_t tidRdr[dimA];
    buffer = malloc(dimA * sizeof(char*));
    temp = malloc(dimA * dimB * sizeof(char));
    for (i = 0; i < dimA; i++) {
      buffer[i] = temp + (i * dimB);
    }
//Create thread Thread
    pthread_create(&tidMpr, NULL, thread, NULL);

    free(temp);
    free(buffer);

    pthread_exit(NULL);
}

2d数组内存分配来自这个问题 - How do I work with dynamic multi-dimensional arrays in C?。 我知道我正在向buffer[0][0]写入所有内容,但这样我就可以基于某些逻辑将each存储在缓冲区数组buffer[0][1]buffer[0][2]中。但现在这应该不是问题吗?

同样line正在打印正确的值,无论它是从stdin读取的。所以,问题可能strtok

另一个非常相似的程序产生所需的输出。这个 -

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef struct { char action; int score; } Rules;
Rules rules[5] = {{'P',50}, {'L',20}, {'D',-10}, {'C',30}, {'S',40}};

int findScore(Rules* rules, char action){
  int i;
  for(i=0; i<5; i++){
    if(rules[i].action == action)
      return rules[i].score;
  }
  fprintf(stderr, "Action not present! Exiting...\n");
  exit(EXIT_FAILURE);

}

int main(int argc, char *argv[]){
  FILE *fp;
  char inputTuple[30] = {};
  char buf[30] = {};
  char* oB;
  oB = "(";
  char* cB;
  char* co = ",";
  cB = ")";
  fp = fopen(argv[1], "r");
  int score;
  char *each;
  size_t len = 0;
  ssize_t read;
  char * line = NULL;
  char *eacharray;
  int u = 0;
  char *outputTuple;
  int pad = 0;
  int g;
  if (fp == NULL)
    exit(EXIT_FAILURE);
  while ((read = getline(&line, &len, fp)) != -1) {

    each = strtok(line," ,()");
    while(each != NULL){

      if(u%3 == 0){
        outputTuple = (char *) malloc(1 + strlen(each)+ strlen(oB) );
        strcpy(outputTuple, oB);
        strcat(outputTuple, each);
      } else if(u%3 == 1){
        char q = *each;
        score = findScore(rules, q);
      } else if(u%3 == 2){
        char * str3 = (char *) malloc(1 + strlen(outputTuple)+ strlen(co) );
        strcpy(str3, outputTuple);
        strcat(str3, co);
        char *str4 = (char *) malloc(1 + strlen(str3)+ strlen(each) );
        strcpy(str4, str3);
        strcat(str4, each);

        for(pad = strlen(each); pad<15; pad++)
          strcat(str4," ");
        sprintf(buf, "%s,%d)\n", str4, score);
        printf("%s", buf);
        free(outputTuple);
        free(str3);
        free(str4);
      }
      each = strtok(NULL," ,()");
      u++;
    }    
    u = 0;
  }
  fclose(fp);

  return 0;
}

更新: strcpy(buffer[0][0], buf);似乎是个问题。当我评论它时,它正在产生输出。我不明白为什么会造成问题。

0 个答案:

没有答案