【问题标题】:Unhandled exception at 0x77ea15de in Assignment 3.exe: 0xC0000005: Access violation reading location 0x33772c58分配 3.exe 中 0x77ea15de 处的未处理异常:0xC0000005:访问冲突读取位置 0x33772c58
【发布时间】:2012-10-11 09:20:22
【问题描述】:

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

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

有人可以帮忙吗?

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);

    }

【问题讨论】:

  • 用调试器单步调试你的代码找出问题所在,然后把相关代码贴在这里。
  • 如果您确定代码在循环中崩溃,您可以发布您的循环代码
  • 已添加循环,但我不确定这是否是问题所在。
  • 您正在使用许多不同的索引变量对数组元素进行大量访问。您是否使用调试器逐步完成此操作,以确保您没有访问超出数组范围的任何元素??
  • 这是您遇到代码问题时要做的第一件事。您正在使用您所说的 Visual Studio - 查看任何帮助文档,以告诉您如何设置断点以及如何逐行执行代码。这使您可以一次执行一行代码,这使您可以查看变量的值并帮助识别错误。它还将允许您找到发生访问冲突的位置。使用调试器对于编程至关重要 - 值得花时间学习如何去做。

标签: c visual-studio-2010 geany


【解决方案1】:

我可以看到以下错误:

  1. 应该是 s[l] = '\0',而不是 s[l+1] = '\0'。
  2. 在 s 中写入横杠后,所有出现的 j 都必须替换为 i。
  3. min_pos 和 max_pos 可能未初始化。

真正的问题是第二个问题。您可以通过养成将变量放在尽可能小的范围内的习惯来避免此类错误。也就是说,你写过这个吗:

        ...
        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) {
        ...

那么代码会更容易理解,如果你在 /* update ... */ 之后错误地使用 j 而不是 i,编译器会给你一个错误。更好的办法是将百分比显示位放在一个单独的函数中。

顺便说一句,格式字符串中不需要这些 %c ,只需将字符直接放入即可。 % 可以用 %% 转义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2014-10-26
    • 2020-12-31
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多