File 我的代码有一个分段错误
这个程序是为读取文件名并显示它们而设计的,但我有一个分段错误。我试图修改代码中的许多内容以使其正常工作,但struct namecords[150000]的数组似乎是我的问题。如果我将该值更改为74000,代码将运行,但如果我将该值增加到150000(我需要的数字),它将不会运行。 这是我的密码:File 我的代码有一个分段错误,file,segmentation-fault,File,Segmentation Fault,这个程序是为读取文件名并显示它们而设计的,但我有一个分段错误。我试图修改代码中的许多内容以使其正常工作,但struct namecords[150000]的数组似乎是我的问题。如果我将该值更改为74000,代码将运行,但如果我将该值增加到150000(我需要的数字),它将不会运行。 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
//structure for storing names in the files
struct NameRecord {
char name[31];
int year;
int frequency;
};
void allCaps(char a[]); //capitalizes all of the characters of s
int getRawData(FILE* fp, struct NameRecord records[], int currSize);//reads in files of the two csv files
void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[]);//calclulates the total population between the 4 year gap
void setNameYearTotals(char theName[], struct NameRecord records[], int size, int nameTotal[]); //stores the frequency for the names in a 4 year difference
void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[]);//gets the name frequency per hundred births for a given year range
void printData(double perHundredThousand[]);//print the frequency of the name per years
void graphPerHundredThousand(double perHundredThousand[]);//display the frequency in a graph like way
main () {
//declarations to be used to get data from user and run functions
int this=0,me,size;
int yearTotal[1000];
int range[150];
int nameTotal[150];
struct NameRecord records[150000];
char name[30];
char theName[150];
char answer;
double thousand[150];
FILE* fp;
do {
printf("Enter a name: ");
scanf("%[^\n]",name);
allCaps(name);
me = getRawData(fp,records,this);
setYearTotals(records,me,yearTotal);
setNameYearTotals(theName,records,me,nameTotal);
getPerHundredThousand(nameTotal,range,thousand);
printData(thousand);
graphPerHundredThousand(thousand);
printf("Do you wish to check another name (Y/N): ");
scanf(" %c",&answer);
} while(answer!='n' && answer !='N');
}
void allCaps(char a[]) {
//uses toupper function in order to capitalize the entered string
int i;
for (i = 0; i < strlen(a); i++) {
a[i] = toupper(a[i]);
}
}
int getRawData(FILE* fp, struct NameRecord records[], int currSize) {
//reads the file names from both male and female records
int i, run =0,temp,j;
currSize=0;
do {
if (run==0) {
fp = fopen("malebabynames.csv", "r");
if(fp == NULL)
printf("File not found:\"malebabynames.csv!\"");
else {
while(fscanf(fp,"%d,%[^,],%d", &records[currSize].year, records[currSize].name, &records[currSize].frequency)!= -1) { //condition to continue reading until end of file is reached
currSize++;
}
fclose(fp);
}
run++;
} else if(run==1) {
fp = fopen("femalebabynames.csv", "r");//change to csv
if(fp == NULL)
printf("File not found:\"femalebabynames.csv!\"");
else {
while(fscanf(fp,"%d,%[^,],%d", &records[currSize].year, records[currSize].name, &records[currSize].frequency)!= -1) {
currSize++;
}
fclose(fp);
}
}
} while(run == 1); //continues till both files are read
//array to sort the file based on year
for(i=0; i < currSize; i++) {
for(j=0; j < currSize; j++) {
if (records[i].year > records[j].year) {
temp = records[i].year;
records[i].year = records[j].year;
records[j].year = temp;
}
}
}
return currSize;
}
void setYearTotals(struct NameRecord records[], int size, int yearRangeTotal[] ) {
//yearRangeTotal[0] holds the total population between 1921-1925. 4 year gap
int k,i,population=0,counter=0;
//loop to hold the frequency
for(k=0; k<size; k++){
for(i=0; i < 4; i++) {
population += records[counter].frequency; //equal to population every 4 years
counter++;
}
yearRangeTotal[k] = population;
population=0;
}
}
void setNameYearTotals(char theName[], struct NameRecord records[], int size, int nameTotal[]) {
// nameTotal[0]. stores the frequency for theName for a 4 year difference
int i,j,counter=0;
//checks for the name, stores in counter and continues checking
for(i = 0; i < size; i++) {
theName[i] = &records[i].name;
for(j = 0; j < 4; j++) {
if(theName[i] == &records[j].name)
counter++;
}
nameTotal[i] = counter;
counter=0;
}
}
void getPerHundredThousand(int nameTotal[], int yearRangeTotal[], double perHundredThousand[]) {
int i,j,k;
double keep;
//gets the name for the frequency of births for a given year period
for(i=0; i < 50; i++) {
keep = 10000* (nameTotal[i]/yearRangeTotal[i]);
perHundredThousand[i] = keep;
}
printData(perHundredThousand);
graphPerHundredThousand(perHundredThousand);
}
void printData(double perHundredThousand[]) {
//print the data starting from 1921 till 2010
int year = 1921,i;
printf("Frequency Per Hundred Thousand Births\n");
printf("=====================================\n");
for(i=0; i != '\0'; i++) {
printf("%d - %d: %lf\n",year,year+4,perHundredThousand[i]);
year+=4;
}
}
void graphPerHundredThousand(double perHundredThousand[]) {
int i,j,temp=0,year=2010;
double stars,k;
double base;
//sorts the array from 2010 to 1921 to get the smallest non-zero value
for(i=0; i != '\0'; i++) {
for(j=0; j < '\0'; j++) {
if (perHundredThousand[i] > perHundredThousand[j]) {
temp = perHundredThousand[i];
perHundredThousand[i] = perHundredThousand[j];
perHundredThousand[j] = temp;
}
}
}
base = perHundredThousand[0];
printf(" Graph\n");
printf("=====================================\n");
//calculates the stars needed in each year based on calculation of the smallest non-zero value calculated from upabove
for(i=0; i != '\0'; i++) {
printf("%d - %d: %lf\n",year,year-4);
stars=perHundredThousand[i]/base;
k = ceil(stars);
for(i=0; i < k; i++) {
if (k == 0)
break;
else {
printf("*");
}
}
year-=4;
}
}
#包括
#包括
#包括
#包括
#包括
//用于在文件中存储名称的结构
结构名称记录{
字符名[31];
国际年;
整数频率;
};
作废所有大写字母(字符a[])//将s的所有字符大写
int getRawData(文件*fp,结构名称记录[],int currSize)//读入两个csv文件的文件
void setYearTotals(结构名称记录[],整数大小,整数年范围总计[])//计算4年间隔期间的总人口
void setNameYearTotals(字符名称[],结构名称记录[],整数大小,整数名称总计[])//以4年的差异存储名称的频率
void getperhundthousand(int-nametottal[],int-yearRangeTotal[],double perhundthousand[])//获取给定年份范围内每百次出生的姓名频率
作废打印数据(每百分之二和[])//每年打印姓名的频率
空隙率千分之二(百分之二和[])//以图形方式显示频率
主要(){
//用于从用户和运行函数获取数据的声明
int this=0,me,size;
国际年总数[1000];
整数范围[150];
国际名称总计[150];
结构名称记录记录[150000];
字符名[30];
字符名称[150];
答案;
双千[150];
文件*fp;
做{
printf(“输入名称:”);
scanf(“%[^\n]”,名称);
allCaps(名称);
me=getRawData(fp、记录、此);
setYearTotals(记录、me、yearTotal);
setNameYearTotals(名称、记录、me、名称总数);
获得百分之一百和(名称总数,范围,千);
打印数据(千);
GraphPerhundred千(千);
printf(“您是否希望检查其他名称(是/否):”;
scanf(“%c”和“应答”);
}while(answer!='n'&&answer!='n');
}
void allCaps(字符a[]){
//使用toupper函数将输入的字符串大写
int i;
对于(i=0;i记录[j].year){
温度=记录[i]。年;
记录[i].年=记录[j].年;
记录[j]。年份=温度;
}
}
}
返回值大小;
}
void setYearTotals(结构名称记录记录[],整数大小,整数年范围总计[]){
//yearRangeTotal[0]保存1921-1925年间的总人口。间隔4年
int k,i,总体=0,计数器=0;
//循环以保持频率
对于(k=0;k/100和[j]){
温度=百分之一百和[i];
每百分之[i]=每百分之[j];
百分之一百和[j]=温度;
}
}
}
基数=每百分之和[0];
printf(“图形\n”);
printf(“==========================================================\n”);
//根据上面计算出的最小非零值计算每年需要的星星数
对于(i=0;i!='\0';i++){
printf(“%d-%d:%lf\n”,年份,第4年);
星=每百分之一和[i]/基;
k=ceil(星星);
对于(i=0;i
几乎可以肯定,您的堆栈溢出了。通过使“records”数组成为main()函数的预定义部分,您将在堆栈上放置至少5850000字节的数据——这几乎肯定大于编译器/链接器/操作系统正在分配的默认堆栈大小。您有三个选项:1)使“记录”数组成为全局数组(这将它放在更大的堆上),2)在运行时使用适当的内存分配函数分配“记录”数组,或3)找出如何让编译器使堆栈更大。这可能会有帮助:很抱歉响应太晚,我有一些检查。我试过了,但是现在我的程序挂起了,而且当我把记录放在一个全局数组中时,它并没有进入一个名字。你真的应该使用一个调试器:当调试器显示出崩溃的代码行时,查找崩溃是很容易的,当你可以在循环中途暂停程序执行时,无限循环很容易被发现。如果由于某种原因您不能使用调试