分段错误核心转储 gcc 代码块
segmentation fault core dumped gcc codeblocks
好吧,我的 ubuntu 系统有问题,我确定我的代码没有任何错误,但我在尝试使用 fseek
时收到 segmentation fault
大文本文件,非常大,因为它是圣经。 gdb 说 fseek.c no such file or directory
。所以有人可以告诉我如何在代码块中为我的程序设置更多内存,因为我认为它的内存问题,或者你有其他想法?
我已经尝试过 usint ulimit
有很多选项,但我不能设置更大的限制
void odpalBiblie()
{
char *file_contents;
long input_file_size;
FILE *input_file = fopen("BIBLIA.TXT", "rb");
fseek(input_file, 0, SEEK_END);
input_file_size = ftell(input_file);
rewind(input_file);
file_contents = malloc(input_file_size * (sizeof(char)));
fread(file_contents, sizeof(char), input_file_size, input_file);
fclose(input_file);
cnt_words(file_contents, 1000);
}
这里是导致分段错误的函数,它恰好发生在 fseek
我认为您不需要更多内存,但您的程序肯定有可能导致一些分段错误,因为您的代码非常不安全,这将是您程序的安全版本
void odpalBiblie()
{
char *file_contents;
long input_file_size;
FILE *input_file;
input_file = fopen("BIBLIA.TXT", "rb");
if (input_file == NULL)
{
fprintf(stderr, "ERROR: el archivo BIBLIA.TXT no se puedo abrir.\n");
return;
}
fseek(input_file, 0, SEEK_END);
input_file_size = ftell(input_file);
rewind(input_file);
/* 1 + input_file_size por el '[=10=]' que debe ir al final */
file_contents = malloc(1 + input_file_size);
if (file_contents == NULL)
{
fprintf(stderr, "ERROR: el sistema no tiene suficiente memoria.\n");
fclose(input_file);
return;
}
/* si vas a usar cualquier función del header strnig.h
* debes sellar el buffer con '[=10=]'.
*/
file_contents[input_file_size] = '[=10=]';
fread(file_contents, 1, input_file_size, input_file);
fclose(input_file);
cnt_words(file_contents, 1000);
}
您必须检查所有可能的故障,例如,如果文件不存在或当前用户不可读,您将有未定义的行为,这可能会导致分段错误。
检查每一个理论上可以失败的功能,以避免出现问题,无论失败发生的可能性有多大。
好吧,我的 ubuntu 系统有问题,我确定我的代码没有任何错误,但我在尝试使用 fseek
时收到 segmentation fault
大文本文件,非常大,因为它是圣经。 gdb 说 fseek.c no such file or directory
。所以有人可以告诉我如何在代码块中为我的程序设置更多内存,因为我认为它的内存问题,或者你有其他想法?
我已经尝试过 usint ulimit
有很多选项,但我不能设置更大的限制
void odpalBiblie()
{
char *file_contents;
long input_file_size;
FILE *input_file = fopen("BIBLIA.TXT", "rb");
fseek(input_file, 0, SEEK_END);
input_file_size = ftell(input_file);
rewind(input_file);
file_contents = malloc(input_file_size * (sizeof(char)));
fread(file_contents, sizeof(char), input_file_size, input_file);
fclose(input_file);
cnt_words(file_contents, 1000);
}
这里是导致分段错误的函数,它恰好发生在 fseek
我认为您不需要更多内存,但您的程序肯定有可能导致一些分段错误,因为您的代码非常不安全,这将是您程序的安全版本
void odpalBiblie()
{
char *file_contents;
long input_file_size;
FILE *input_file;
input_file = fopen("BIBLIA.TXT", "rb");
if (input_file == NULL)
{
fprintf(stderr, "ERROR: el archivo BIBLIA.TXT no se puedo abrir.\n");
return;
}
fseek(input_file, 0, SEEK_END);
input_file_size = ftell(input_file);
rewind(input_file);
/* 1 + input_file_size por el '[=10=]' que debe ir al final */
file_contents = malloc(1 + input_file_size);
if (file_contents == NULL)
{
fprintf(stderr, "ERROR: el sistema no tiene suficiente memoria.\n");
fclose(input_file);
return;
}
/* si vas a usar cualquier función del header strnig.h
* debes sellar el buffer con '[=10=]'.
*/
file_contents[input_file_size] = '[=10=]';
fread(file_contents, 1, input_file_size, input_file);
fclose(input_file);
cnt_words(file_contents, 1000);
}
您必须检查所有可能的故障,例如,如果文件不存在或当前用户不可读,您将有未定义的行为,这可能会导致分段错误。
检查每一个理论上可以失败的功能,以避免出现问题,无论失败发生的可能性有多大。