分配3.exe中0x77ea15de处未处理的异常:0xC0000005:访问冲突读取位置0x33772c58

分配3.exe中0x77ea15de处未处理的异常:0xC0000005:访问冲突读取位置0x33772c58,c,visual-studio-2010,geany,C,Visual Studio 2010,Geany,我不明白这个错误告诉了我什么。当我运行程序时,它应该持续循环,直到用户输入N(表示不再/退出)。我不明白发生了什么事。在第一组输入之后,在提示用户“您想处理一个新学生吗?”之前,程序抛出该异常 我使用Geany(它没有给我任何错误定义,只是崩溃了)和VisualStudio2010运行了它 有人能帮忙吗 void draw_bar_chart(int student_id[], int percentage[], int size) { FILE *report; int i,

我不明白这个错误告诉了我什么。当我运行程序时,它应该持续循环,直到用户输入N(表示不再/退出)。我不明白发生了什么事。在第一组输入之后,在提示用户“您想处理一个新学生吗?”之前,程序抛出该异常

我使用Geany(它没有给我任何错误定义,只是崩溃了)和VisualStudio2010运行了它

有人能帮忙吗

void draw_bar_chart(int student_id[], int percentage[], int size)
{
    FILE *report;
    int i, j=0, min = 1000000, max = 0, min_pos, max_pos, l;
    char s[20];

    report = fopen("report2.txt", "a");
    fprintf(report, "\n******* BAR CHART (YEARLY TOTAL EXPENSES: YEARLY TOTAL INCOME) *******\n\n");      

    for (i = 0; i < size; i++)
    {
        fprintf(report, "%d%c", student_id[i], ' ');
        if (percentage[i] > 0){
            l = percentage[i]/10; //the lenght of the bar
            if ((percentage[i]%10) > 0)
                l++;
            for (j = 0; j < l; j++)
                s[j] = 'x';
            s[l+1] = '\0';
        } else {
            s[0] = '!';
            s[1] = '\0';
        }

        fprintf(report, "%-20s%6c%d%c\n", s, ' ', percentage[j], '%');

        if (percentage[j] >= 0)
        {
            if (percentage[j] < min)        
            {
                min = percentage[j];        
                min_pos = j;                
            }
            if (percentage[j] > max)    
            {
                max = percentage[j];        
                max_pos = j;                
            }
        }
    }


    fprintf(report, "***lowest percentage:  %d%c (student ID: %d)\n", min, '%', student_id[min_pos]);
    fprintf(report, "***highest percentage: %d%c (student ID: %d)\n", max, '%', student_id[max_pos]);

    fclose(report);

    }
void draw\u bar\u图表(整数学生id[],整数百分比[],整数大小)
{
文件*报告;
int i,j=0,min=1000000,max=0,min_pos,max_pos,l;
chars[20];
report=fopen(“report2.txt”,“a”);
fprintf(报告,“\n*******条形图(年度总费用:年度总收入)*******\n\n”);
对于(i=0;i0){
l=百分比[i]/10;//棒材的长度
如果((百分比[i]%10)>0)
l++;
对于(j=0;j=0)
{
如果(百分比[j]最大值)
{
最大值=百分比[j];
最大位置=j;
}
}
}
fprintf(报告,***最低百分比:%d%c(学生ID:%d)\n),最小,“%”,学生ID[min\u pos]);
fprintf(报告,***最高百分比:%d%c(学生ID:%d)\n),最大,“%”,学生ID[max\u pos]);
fclose(报告);
}

我可以看到以下错误:

  • 它应该是s[l]='\0',而不是s[l+1]='\0'
  • 在将条形图写入s之后,所有出现的j必须替换为i
  • 最小位置和最大位置可能未初始化
  • 真正的问题是第二个。通过养成将变量放在尽可能小的范围内的习惯,可以避免此类错误。i、 e.你写过这个吗:

            ...
            fprintf(report, "%d%c", student_id[i], ' ');
    
            /* display percentage */
            {
              char s[20];
    
              if (percentage[i] > 0) {
                int l, j;
    
                l = percentage[i] / 10; // the length of the bar
                if ((percentage[i]%10) > 0)
                    l++;
                for (j = 0; j < l; j++)
                    s[j] = 'x';
                s[l] = '\0';
              }
              else {
                s[0] = '!';
                s[1] = '\0';
              }
    
              fprintf(report, "%-20s%6c%d%c\n", s, ' ', percentage[i], '%');
            }
    
            /* update min, max */
            if (percentage[i] >= 0) {
            ...
    
    。。。
    fprintf(报告,“%d%c”,学生id[i],”;
    /*显示百分比*/
    {
    chars[20];
    如果(百分比[i]>0){
    int l,j;
    l=百分比[i]/10;//棒的长度
    如果((百分比[i]%10)>0)
    l++;
    对于(j=0;j=0){
    ...
    
    如果在/*update…*/之后错误地使用了j而不是i,那么代码会更容易理解,编译器会给您一个错误。更好的方法是将百分比显示位放在一个单独的函数中


    顺便说一句,您不需要这些%c格式的字符串,只需直接输入字符即可。可以使用%%进行转义。

    使用调试器逐步检查代码以查找问题,然后在此处发布相关代码。如果您确定代码在循环中崩溃,您可以发布已添加的looploop代码,我不确定这是否正确但是他提出了一个问题。您正在使用大量不同的索引变量访问数组元素。您是否已使用调试器逐步完成此操作,以确保您没有访问超出数组边界的任何元素?这是您遇到代码问题时要做的第一件事。您正在使用visual studio,您可以说-签出任何帮助文档都可以告诉您如何设置断点以及如何逐行执行代码。这使您可以一次执行一行代码,从而可以查看变量的值并帮助识别错误。它还可以让您定位访问冲突发生的位置。使用调试器对编程至关重要-它值得花时间去学习如何做。