尝试从 .txt 文件复制整数时出现分段错误
segmentation fault when trying to copy integers from .txt file
因此,对于我的学校课程,我只需从格式如下的文本文件中读取整数列表,第一行指示整数的数量。然后对整数进行排序(它们在 txt 文件中未排序),然后求平均值。尽管我一直在使用测试 .txt 文件,但我只是试图将整数读入数组。
9
1
3
2
4
...
#include<stdio.h>
#include<stdlib.h>
int amtValues;
int *values;
float find_median (int* values){
if(amtValues%2==0){
return ((values[amtValues/2] + values[amtValues/2 -1]) /2.0);
}
else
return values[amtValues/2];
}
int main(int argc, char **argv){
//open the file, copy the data from the file, and determine the number of values
//open the filestream
FILE* fp = fopen(argv[1], "r");
if(!fp){
printf("error reading file\n");
return 1;
}
//determine the number of values
char *lineOne;
if(fgets(lineOne, 80, fp)!=1)
puts (lineOne);
lineOne++;
amtValues = atoi(lineOne);
printf("\nThe amount of values is: %d\n",amtValues);
/*allocate memory for values array, and copy the values from file.*/
values = (int*)malloc(amtValues*sizeof(int));
int i=0;
while(!feof(fp)){
int curNum;
fscanf(fp, "%d", &curNum);
values[i] = curNum;
i++;
}
fclose(fp);
for(i=0; i<amtValues; i++)
printf("\n%d");
}
这就是我目前正在做的事情,我收到一条错误消息,提示 segmentationFault(核心已转储)。我是 C 的新手,所以我真的不确定那是什么意思。
我没有检查你的整个代码,但这里导致段错误:
//determine the number of values
char *lineOne;
if(fgets(lineOne, 80, fp)!=1)
puts (lineOne);
您需要有 lineOne
的记忆。只有一个指针并不意味着你有缓冲区的内存,你需要在堆或堆栈上分配它。像这样尝试:
char lineOne[81]; // allocation on stack
或
char *lineOne = malloc(sizeof(*lineone) * 81); // allocate on heap
I get an error saying: segmentation fault (core dumped)
A segmentation fault is practically some undefined behavior (UB), and means that your program is buggy, because the computer is dereferencing some invalid memory address (outside of the virtual address space of your process).
(我猜你在 Linux 或其他一些 POSIX 系统上,但你的问题中应该提到)
一个core dump (on Linux, see core(5) for details) is a file -generally named core
- describing the state of the faulty process (when the fault happens, see signal(7)), notably its virtual address space. You can use the gdb
debugger to debug post-mortem a core dump. You could also disable core dumps (but you better not, they are very useful), e.g. with ulimit -c
bash builtin (or setrlimit(2)系统调用)。
然而,并不是所有未定义的行为都会给出这样的错误。你很幸运能得到一个。它可能会发生(例如,因为 ASLR) that another run of the same buggy executable 不会发生核心转储或段错误(但会有一些更糟糕的行为)。
实践中:
编译所有警告和调试信息:gcc -Wall -Wextra -g
和 GCC。改进您的代码以完全没有警告(在您的情况下,您的代码会收到一些警告)。
阅读文档,尤其是您的compiler, and of every function you are using (e.g. you are using fgets
wrongly: it will never give 1). Later, you could even read the C11 standard n1570。
了解如何use the gdb
debugger to understand the behavior of your faulty program. This is an essential skill to acquire. You should have tests。
了解更多关于UB,对scared of them. The worst UB is the one that apparently don't (always) give a fault. Tools like valgrind很有帮助。
也许您想使用 getline(3). See this. Maybe you could have a flexible array member, like here and with some abstract data type。
还要注意 Halting Problem and its undecidability。
PS。所有开发人员都会制造错误。找到它们具有挑战性,但可能很有趣。但是编程是difficult.
因此,对于我的学校课程,我只需从格式如下的文本文件中读取整数列表,第一行指示整数的数量。然后对整数进行排序(它们在 txt 文件中未排序),然后求平均值。尽管我一直在使用测试 .txt 文件,但我只是试图将整数读入数组。
9
1
3
2
4
...
#include<stdio.h>
#include<stdlib.h>
int amtValues;
int *values;
float find_median (int* values){
if(amtValues%2==0){
return ((values[amtValues/2] + values[amtValues/2 -1]) /2.0);
}
else
return values[amtValues/2];
}
int main(int argc, char **argv){
//open the file, copy the data from the file, and determine the number of values
//open the filestream
FILE* fp = fopen(argv[1], "r");
if(!fp){
printf("error reading file\n");
return 1;
}
//determine the number of values
char *lineOne;
if(fgets(lineOne, 80, fp)!=1)
puts (lineOne);
lineOne++;
amtValues = atoi(lineOne);
printf("\nThe amount of values is: %d\n",amtValues);
/*allocate memory for values array, and copy the values from file.*/
values = (int*)malloc(amtValues*sizeof(int));
int i=0;
while(!feof(fp)){
int curNum;
fscanf(fp, "%d", &curNum);
values[i] = curNum;
i++;
}
fclose(fp);
for(i=0; i<amtValues; i++)
printf("\n%d");
}
这就是我目前正在做的事情,我收到一条错误消息,提示 segmentationFault(核心已转储)。我是 C 的新手,所以我真的不确定那是什么意思。
我没有检查你的整个代码,但这里导致段错误:
//determine the number of values
char *lineOne;
if(fgets(lineOne, 80, fp)!=1)
puts (lineOne);
您需要有 lineOne
的记忆。只有一个指针并不意味着你有缓冲区的内存,你需要在堆或堆栈上分配它。像这样尝试:
char lineOne[81]; // allocation on stack
或
char *lineOne = malloc(sizeof(*lineone) * 81); // allocate on heap
I get an error saying: segmentation fault (core dumped)
A segmentation fault is practically some undefined behavior (UB), and means that your program is buggy, because the computer is dereferencing some invalid memory address (outside of the virtual address space of your process).
(我猜你在 Linux 或其他一些 POSIX 系统上,但你的问题中应该提到)
一个core dump (on Linux, see core(5) for details) is a file -generally named core
- describing the state of the faulty process (when the fault happens, see signal(7)), notably its virtual address space. You can use the gdb
debugger to debug post-mortem a core dump. You could also disable core dumps (but you better not, they are very useful), e.g. with ulimit -c
bash builtin (or setrlimit(2)系统调用)。
然而,并不是所有未定义的行为都会给出这样的错误。你很幸运能得到一个。它可能会发生(例如,因为 ASLR) that another run of the same buggy executable 不会发生核心转储或段错误(但会有一些更糟糕的行为)。
实践中:
编译所有警告和调试信息:
gcc -Wall -Wextra -g
和 GCC。改进您的代码以完全没有警告(在您的情况下,您的代码会收到一些警告)。阅读文档,尤其是您的compiler, and of every function you are using (e.g. you are using
fgets
wrongly: it will never give 1). Later, you could even read the C11 standard n1570。了解如何use the
gdb
debugger to understand the behavior of your faulty program. This is an essential skill to acquire. You should have tests。了解更多关于UB,对scared of them. The worst UB is the one that apparently don't (always) give a fault. Tools like valgrind很有帮助。
也许您想使用 getline(3). See this. Maybe you could have a flexible array member, like here and
还要注意 Halting Problem and its undecidability。
PS。所有开发人员都会制造错误。找到它们具有挑战性,但可能很有趣。但是编程是difficult.