简体   繁体   English

为什么当我向 main 函数添加任何语句时,我的 C 程序会崩溃?

[英]Why does my C program crash when i add any statement to the main function?

I'm reasonably new to C - I've made a program that reads a text file and through several functions processes the text by using malloc() and realloc() and formats an output.我对 C 相当陌生 - 我制作了一个程序,它读取一个文本文件,并通过几个函数使用malloc()realloc()处理文本并格式化输出。 At the moment I only have the following in my main() :目前我的main()只有以下内容:

1. initializing the structs 1. 初始化结构

  line_arr.max_size = 0;
  line_arr.num_lines = 0;
  line_arr.line = NULL;

  Word_Array word_arr;
  word_arr.max_size_ = 0;
  word_arr.num_words = 0;
  word_arr.word = NULL;

  Word_Array ex_words;
  ex_words.max_size_ = 0;
  ex_words.num_words = 0;
  ex_words.word = NULL; 

Line_Array.line and Word_Array.word are both pointers to structs Line and Word , respectively. Line_Array.lineWord_Array.word都是指向LineWord结构的指针。 Line and Word are structs that hold a char array and an int that keeps track of what line number the line is or the word occurs in. Line 和 Word 是包含一个 char 数组和一个 int 的结构体,用于跟踪行号或单词出现的行号。

2. calling the functions that process the input text files 2.调用处理输入文本文件的函数

  get_lines(&line_arr, argv[1]);
  get_words(&line_arr, &word_arr);
  sort_keywords(&word_arr);

All of my functions return void .我的所有函数都返回void

All the output and formatting occurs within the functions and any space I have malloc 'd I have subsequently freed without any error.所有的输出和格式都发生在函数和我有malloc的任何空间中,我随后没有任何错误地释放了它。 The program produces the desired output, however if I add any statement to the main() even a print statement, I get a bus10 error or a segmentation fault 11 error.该程序生成所需的输出,但是如果我向main()添加任何语句,甚至是打印语句,我都会收到bus10错误或segmentation fault 11错误。

I have tried gdb but I haven't used it before, and it seems as though gdb hangs and freezes when I try to run the program anyway.我已经尝试过 gdb,但我以前没有使用过它,而且当我尝试运行该程序时,gdb 似乎挂起并冻结了。

I'm wondering if anyone knows why this is the case?我想知道是否有人知道为什么会这样? / is there some fundamental logic I'm not understanding about C or malloc() ? / 是否有一些我不了解 C 或malloc()基本逻辑?

Thanks for your expertise!感谢您的专业知识!

edit structs:编辑结构:

typedef struct Line Line;
struct Line{
  char line[MAX_LINE_LEN];
};

typedef struct Word{
  char word[MAX_WORD_LEN];
  int line_number;
}Word;

typedef struct Word_Array{
  //will point to the base in memory where the list of words begins.
  Word *word;
  int max_size_;
  int num_words;
}Word_Array;

typedef struct Line_Array{
  //line is a variable that stores an address of a line object.
    Line *line;
    int max_size;
    int num_lines;
}Line_Array;

get_lines() : get_lines()

void get_lines(Line_Array *la, char const *filename){
  Line *line_ptr;
  char *buffer;
  size_t buffer_len;

  FILE *fp = fopen(filename, "r");
  if(fp == NULL){
    printf("Can't read file. \n");
    exit(1);
  }

  while (getline(&buffer, &buffer_len, fp) > 0) {
    buffer[strlen(buffer)-1] = '\0';
    if (la->line == NULL) {
      la->line = (Line *) malloc(sizeof(Line));
      if (la->line == NULL) {
        exit(1);
      }
      la->max_size = 1;
      la->num_lines = 0;
    }

    else if (la->num_lines >= la->max_size) {
      line_ptr = (Line *) realloc(la->line, (2*la->max_size) * sizeof(Line));
      if (line_ptr == NULL) {
        exit(1);
      }

      la->max_size *= 2;
      la->line = line_ptr;

    }

    strncpy(la->line[la->num_lines].line, buffer, MAX_LINE_LEN);
    la->num_lines++;
  }
  fclose(fp);
}

I haven't freed the memory in this method since I make use of it later, but even when other functions aren't being run the same problem exists where if I add something to the main before or after calling get_lines, I receive bus 10 error as my only output.自从我稍后使用它以来,我没有释放此方法中的内存,但是即使没有运行其他函数,也会存在同样的问题,如果我在调用 get_lines 之前或之后向 main 添加某些内容,我会收到总线 10错误作为我唯一的输出。 However if i only call get_lines() and other functions the program produces the right output.但是,如果我只调用get_lines()和其他函数,程序会产生正确的输出。

A least one problem:至少有一个问题:

Variables need to be initialized before getline() usage.在使用getline()之前需要初始化变量。 @Weather Vane @风向标

  //char *buffer;
  //size_t buffer_len;
  char *buffer = NULL;
  size_t buffer_len = 0;

Notes:笔记:

After the while (getline(... , code should free with free(buffer);while (getline(... , code should free with free(buffer);

strncpy(la->line[la->num_lines].line, buffer, MAX_LINE_LEN); does not insure la->line[la->num_lines].line is a string .不保证la->line[la->num_lines].linestring Why is strncpy insecure? 为什么 strncpy 不安全?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在c中调用函数时程序崩溃? - Why does the program crash when the function is called in c? 当我尝试重新分配()结构指针数组时,为什么我的 C 程序会崩溃? - Why does my C program crash when I try to realloc() a pointer array of structs? 当我尝试从单链表中删除节点时,为什么我的 C 程序崩溃 - Why does my C Program crash when I try to delete a node out of a singe linked list 尝试在函数内添加节点但在main函数中工作时,为什么我的程序崩溃 - Why does my program crashes when I tried adding a node inside a function but works in the main function 尝试使用perror函数时,为什么我的程序崩溃了? - Why did my program crash when I tried the perror function? 为什么我的C程序崩溃? - Why does my C program crash? 为什么向函数添加更多参数时内核会崩溃? - Why does my kernel crash when I add more arguments to a function? 为什么该程序在运行时崩溃? - Why does this program crash when I run it? 为什么我的PostgreSQL C函数崩溃了? - Why does my PostgreSQL C function crash? 为什么我的用于将元素插入到哈希树中的C代码在Main()中起作用,但是当我通过函数调用它时却不起作用? - Why does my C code for inserting an element into a hash tree work in Main() but not when I call it via function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM