C代码分段错误

C代码分段错误,c,pointers,allocation,C,Pointers,Allocation,我试图编译我的c代码,但我总是在执行程序后出现分段错误。以下是我的部分代码: LINE_LENGTH=300 struct clip { int views; char *user; char *id; char *title; char *duration; struct clip *next; } *head; 我的主要功能,其中argv[1]是我的csv文件 int main(int argc, char **argv) { int n; head

我试图编译我的c代码,但我总是在执行程序后出现分段错误。以下是我的部分代码:

LINE_LENGTH=300

struct clip {
  int views;
  char *user;
  char *id;
  char *title;
  char *duration;
  struct clip *next;
} *head;
我的主要功能,其中argv[1]是我的csv文件

int main(int argc, char **argv) {
   int n;
   head = build_a_lst(*(argv+1));
   return 0;}
我剩下的代码

struct clip *build_a_lst(char *fn) {
  FILE *fp;
  struct clip *hp;
  char *fields[5];
  char line[LINE_LENGTH];
  int cnt=0,i;
  hp=NULL;

  fp=fopen(fn,"r");
  if(fp=NULL)
    exit(EXIT_FAILURE);
  while(fgets(line,LINE_LENGTH,fp)!=NULL){
    split_line(fields,line);//fields has five values stored
    hp=append(hp,fields);
    for(i=0;i<5;i++){
      free(fields[i]);
      fields[i]=NULL;
    }
  }

  return hp;
}


void split_line(char **fields,char *line) {
  int i=0;
  char *token, *delim;
  delim = ",\n";

  token=strtok(line,delim);//ok line
  for(;token!=NULL;i++){

    fields[i]=malloc(strlen(token)+1);
    strcpy(fields[i],token);
    token=strtok(NULL,delim);
  }

}

struct clip *append(struct clip *hp,char **five) {
  struct clip *cp,*tp;

  tp=malloc(sizeof(struct clip));
  tp->views=atoi(five[1]);

  tp->user=malloc(strlen(five[0]+1));
  tp->duration=malloc(strlen(five[2]+1));
  tp->id=malloc(strlen(five[3]+1));
  tp->title=malloc(strlen(five[4]+1));

  strcpy(tp->user,five[0]);
  strcpy(tp->duration,five[2]);
  strcpy(tp->id,five[3]);
  strcpy(tp->title,five[4]);

  cp=hp;
  while(cp!=NULL)
    cp=cp->next;

  cp->next=tp;
  hp=cp;

  return hp;
}
struct clip*build\u a\u lst(char*fn){
文件*fp;
结构剪辑*hp;
字符*字段[5];
字符行[行长度];
int cnt=0,i;
hp=NULL;
fp=fopen(fn,“r”);
如果(fp=NULL)
退出(退出失败);
while(fgets(行,行长度,fp)!=NULL){
split_line(fields,line);//字段存储了五个值
hp=追加(hp,字段);
对于(i=0;iviews=atoi(五[1]);
tp->user=malloc(strlen(五[0]+1));
tp->duration=malloc(strlen(五[2]+1));
tp->id=malloc(strlen(五[3]+1));
tp->title=malloc(strlen(五[4]+1));
strcpy(tp->user,五[0]);
strcpy(tp->持续时间,五[2]);
strcpy(tp->id,五[3]);
strcpy(tp->title,五[4]);
cp=hp;
while(cp!=NULL)
cp=cp->next;
cp->next=tp;
hp=cp;
返回hp;
}

根据一些文章,分段错误是由于尝试读取或写入非法内存位置造成的。由于我在代码的不同部分分配内存,因此问题应该存在。请有人帮助我解决此问题。提前谢谢。

您的代码有一些问题:

  • if(fp=NULL)
    应该是
    if(fp=NULL)
  • char*字段[5];
    应该是
    char*字段[5]={NULL};
  • for(;token!=NULL;i++){
    应该是
    for(;token!=NULL&&i<5;i++){
  • 这些:

    tp->user=malloc(strlen(five[0]+1));
    tp->duration=malloc(strlen(five[2]+1));
    tp->id=malloc(strlen(five[3]+1));
    tp->title=malloc(strlen(five[4]+1));
    
    应该是

    tp -> user     = malloc(strlen(five[0]) + 1);
    tp -> duration = malloc(strlen(five[2]) + 1);
    tp -> id       = malloc(strlen(five[3]) + 1);
    tp -> title    = malloc(strlen(five[4]) + 1);
    
  • 您不会
    释放
    多个
    malloc
    ed内存

  • 尝试使用gdb运行,并将回溯(bt)粘贴粘贴到可用于验证问题的最小值。启用警告至少会发现一个问题。对于gcc和clang,请使用
    -Wall-Wextra
    (对于其他编译器,请使用其他内容)。让我们从第一行开始:
    line_LENGTH=300
    ,缺少一个分号,如果没有进一步的上下文,几乎肯定是编译器错误。如果他启用了警告,第一行很容易被捕获谢谢你指出这些问题。我更新了代码,但仍然存在分段错误message@JOrG这很容易如果你发布了一个。并且不要编辑你的问题,使所有的答案变得毫无意义。你可以使用调试器来解决这个问题。