C:程序在完成循环之前崩溃

时间:2012-03-29 02:58:39

标签: c arrays for-loop io crash

Hello Stackoverflow工作人员。我是一个非常业余的C程序员,我正在编写一个程序,读取有关结婚礼物的一些信息,然后输出包含最大礼品价值,最低礼品价值,礼品价值的总平均值以及在x> 1处估价的礼物的平均值我已经完成了所有内容的编写,但程序在第一次循环后似乎总是崩溃。我在过去的几个小时里一直在关注它,所以我在找出错误可能是什么问题。这是我的代码:

#include <stdio.h>
#include <stdlib.h>


int main() {

    //Opens the file and creats a pointer for it.
    FILE *ifp;
    ifp = fopen("gifts.txt", "r");

    //Declares the variables
    int i, j, k, l, m, n, o, p, q, x, y;
    int gift_sets, num_gifts, prices, max_value, max, avg_val, no_zero;

    //Scans the file and assigns the first line to variable "gift_sets"
    fscanf(ifp, "%d", &gift_sets);

    //Begins a for loop that repeats based on the value of gift_sets
    for (i = 0; i < gift_sets; i++) {

        printf("Wedding Gifts #%d\n", i + 1);
        printf("Gift Value\t Number of Gifts\n");
        printf("----------\t ---------------\n");

        //Scans the price values into the array prices[num_gifts]
        fscanf(ifp, "%d", &num_gifts);
        int prices[num_gifts];

        //Creates a loop through the prices array
        for (j = 0; j < num_gifts; j++){
            fscanf(ifp, "%d", &prices[j]);
        }

        //Declares a frequency array
        int freq[max + 1];

        for (k = 0; k <= max; k++) {
            freq[k] = 0;
        }

        for (l = 0; l < num_gifts; l++) {
            freq[prices[l]]++;
        }
        for (m = 0; m < max + 1; m++) {
            if (freq[m] > 0){
                printf("%d\t%d",m, freq[m]);
            }
        }

        printf("\n");

        //Zeroes the variable "max_val."
        int max_val = prices[0];

        //Loops through the array to find the maximum gift value.
        for (n = 0; n < num_gifts; n++){
            if (prices[n] > max_value)
                max_value = prices[n];
        }

        // Zeroes "min_val."
        int min_val = prices[0];

        //Finds the lowest value within the array.
        for(o = 0; o < num_gifts; o++){
            if(prices[o] !=0){
                if(prices[o] < min_val){
                    min_val = prices[o];
                 }
             }
        }

        //Calculates the total number of gifts.
        double sum_gifts = 0;
        for(p = 0; p < num_gifts; p++){
            sum_gifts = sum_gifts + prices[p];
        }

        //Calculates the average value of all the gifts.
        avg_val =  (sum_gifts / num_gifts);

        //find non zero average
        double x = 0;
        int y = 0;
        for(q = 0; q < num_gifts; q++){
            if (prices[q] != 0){
                x += prices[q];
                y++;
            }
        }

        //Calculates the average value of the gifts, excluding the gifts valued zero.
        int no_zero = x / y;

        //Prints the maximum gift value.
        printf("The maximum gift value is: $%d", max_value);
        printf("\n");

        //Prints the minimum gift value.
        printf("The minimum gift value is: $%d\n", min_val);


        //Prints the average of all the gifts.
        printf("The average of all gifts was $%.2lf\n",avg_val);

        //Prints the no zero average value of the gifts.
        printf("The average of all non-zero gifts was $%.2lf",no_zero);
        printf("\n\n\n");

    }

    return 0;
}

提前感谢帮助人员。一如既往,非常感谢。

编辑:进一步说明,“崩溃”是执行程序时“gift.exe已停止工作”的Windows错误。它确实在窗口底部说“处理返回-1073741819&lt; 0xC0000005&gt;”

3 个答案:

答案 0 :(得分:3)

当您使用num_gifts变量声明数组时,它会生成汇编指令,这些指令在堆栈上分配足够的空间来容纳num_gifts整数。它在编译时执行此操作。通常这不会编译,但是根据ms c编译器的行为,它可以编译并假设默认情况下放在num_gifts中的任何值(可能是0,可能是别的)是长度。当您访问它时,您可能正在尝试访问具有零元素的数组,这可能会导致访问冲突。

答案 1 :(得分:2)

我会马上告诉你应该做的一件事。

检查fscanf及其兄弟的返回值。如果由于某种原因扫描失败,这将返回少于您的预期(它返回成功扫描的项目数)。

在这种情况下,您的数据文件不是您的代码所期望的。

您还应该检查ifp是否为NULL - 这可能是因为您无论如何盲目地使用它。

您在IDE中可以找到的一件事是您可能不在您认为自己所在的目录中(特别是gifts.txt所在的目录。)

最重要的是,max设置为任意值,因此int freq[max+1];将为您提供一个不确定大小的数组。如果该大小小于最大价格,您将使用以下内容修改超出数组末尾的内存:

freq[prices[l]]++;

这是一个明确的禁忌,“未定义的行为”领域。

答案 2 :(得分:1)

至少乍一看,在您(尝试)使用它来定义max数组之前,您似乎尚未初始化freq

相关问题