gpt4 book ai didi

c - 当我在 Linux 中运行这段代码时,为什么它总是给我一个段错误?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:52 24 4
gpt4 key购买 nike

所以我试图创建一个函数,它接受一个文本文件,其中包含一堆由换行符分隔的单词,并将文本文件读入 char** 数组。

当我在 Windows 上的 netbeans 中运行此代码时,它工作正常,但如果我在 Linux 中运行它,我会遇到段错误。

// globals
FILE *words_file;
char **dic;
int num_words = 0;

void read_to_array() {
words_file = fopen("words.txt", "r");
char *line = NULL;
int i = 0;
size_t len = 0;
dic = (char **)malloc(99999 * sizeof(char *));

// read dic to array
while (getline(&line, &len, words_file) != -1) {
dic[i] = (char*)malloc(len);
strcpy(dic[i], line);

// get rid of \n after word
if (dic[i][strlen(dic[i]) - 1] == '\n') {
dic[i][strlen(dic[i]) - 1] = '\0';
}
++i;
num_words++;
}
//printf("%s", dic[i][strlen(dic[i]) - 1]); //testing
fclose(words_file);
dic[i] = NULL;
}

我在这里错过了什么?

最佳答案

您的程序中存在一些问题,可能会导致您观察到未定义的行为:

  • 您不测试文件是否已成功打开,如果文件不在您期望的位置或具有不同的名称,则会导致未定义的行为。
  • 您不限制读入数组的行数,如果文件包含超过 99998 行会导致未定义的行为,在 linux 中可能是这种情况 /usr/share/dict/words例如,我的系统上有 139716 行。

您的内存分配方案不是最优但正确:您应该计算单词的长度并在分配副本之前去除换行符。按照编码,您分配了太多内存。然而你应该释放lineread_to_array 回来之前并且您应该避免使用全局变量。

修改后的版本:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **read_to_array(const char *filename, int *countp) {
FILE *words_file;
char *line = NULL;
size_t line_size = 0;
char **dic = NULL;
int dic_size = 0;
int i = 0;

words_file = fopen(filename, "r");
if (words_file == NULL) {
fprintf(stderr, "cannot open dictionary file %s\n", filename);
return NULL;
}

dic_size = 99999;
dic = malloc(dic_size * sizeof(char *));
if (dic == NULL) {
fprintf(stderr, "cannot allocate dictionary array\n");
fclose(words_file);
return NULL;
}

// read dic to array
while (getline(&line, &line_size, words_file) != -1) {
size_t len = strlen(line);
/* strip the newline if any */
if (len > 0 && line[len - 1] == '\n') {
line[--len] = '\0';
}
if (i >= dic_size - 1) {
/* too many lines: should reallocate the dictionary */
fprintf(stderr, "too many lines\n");
break;
}
dic[i] = malloc(len + 1);
if (dic[i] == NULL) {
/* out of memory: report the error */
fprintf(stderr, "cannot allocate memory for line %d\n", i);
break;
}
strcpy(dic[i], line);
i++;
}
dic[i] = NULL;
*countp = i;
fclose(words_file);
free(line);
return dic;
}

int main(int argc, char **argv) {
const char *filename = (argc > 1) ? argv[1] : "words.txt";
int num_words;
char **dic = read_to_array(filename, &num_words);
if (dic != NULL) {
printf("dictionary loaded: %d lines\n", num_words);
while (num_words > 0)
free(dic[--num_words]);
free(dic);
}
return 0;
}

输出:

chqrlie> readdic /usr/share/dict/wordstoo many linesdictionary loaded: 99998 lines

关于c - 当我在 Linux 中运行这段代码时,为什么它总是给我一个段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55560841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com