【问题标题】:Segmention Fault while in the middle of nested for loop嵌套for循环中间出现分段错误
【发布时间】:2016-08-04 22:07:14
【问题描述】:

我编写了一个代码,需要从用户指定的数据文件(这可能是多达 10,000 行数据)中读取一系列值,这是在第一个 for 循环中完成的。在每一行值中,一个变量被转换为一个字符串,该字符串定义了要加载的另一组输入文件(每个包含约 20,000 行数据),这些文件在第二个 for 循环中用于计算。这适用于前几百次迭代......它正确读取所有内容,并且结果计算(在下面的代码中省略了计算,因为代码在有或没有它们的情况下都会产生相同的问题)与预期的一样。

问题在于,当第一个 for 循环达到大约 600 次迭代时,它会停止并产生错误:segmentation fault(core dumped)

我很确定这是某种内存问题,因为如果我通过在第二个循环中省略 4 个输入文件来测试代码,并且它能够达到更高的迭代次数......

这是代码:

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

int main()

{
    int num, i, j;
    double X, Y;// 
    float Z;
    char the_data[25], Z_name[10], input_1[50], input_2[50], input_3[50], input_4[50], input_5[50];
    double a, b, c, d, e;

    printf("Enter the name of the data file you are using:\n");
    scanf("%24s", &the_data);
    FILE *DATA = fopen(the_data,"r");

    for (j=1; j<800; j++)
        {

            //***************Read data from a file, the variable Z is a floating point number which, when rounded to the nearest decimal place,  
            //********************determines which directory to load model data from for each value of X and Y
            fscanf(DATA, "%lf %lf %f\n", &X, &Y, &Z);

            //round Z to the nearest 1 decimal place
            Z = Z * 10;
            Z = roundf(Z);
            Z = Z / 10;

            //assign the full directory name to Z_name
            sprintf(Z_name, "%.01fdirectory", Z);

            //assign Z_name to input name string for path to the appropriate data file locations
            sprintf(input_1, "./%s/a.txt", Z_name);
            sprintf(input_2, "./%s/b.txt", Z_name);
            sprintf(input_3, "./%s/c.txt", Z_name);
            sprintf(input_4, "./%s/d.txt", Z_name);
            sprintf(input_5, "./%s/e.txt", Z_name);

            //Open the files
            FILE *input1 = fopen(input_1, "r");
            FILE *input2 = fopen(input_2, "r");
            FILE *input3 = fopen(input_3, "r");
            FILE *input4 = fopen(input_4, "r");
            FILE *input5 = fopen(input_5, "r");


            for (i=1; i < 10000; i++)
                {
                    //For a given Z value, read in the corresponding values. Usually these input files have ~20000 values in each so the loop would be set to run until the end of the file
                    fscanf(input1, "%lf", &a);
                    fscanf(input2, "%lf", &b);
                    fscanf(input3, "%lf", &c);
                    fscanf(input4, "%lf", &d);
                    fscanf(input5, "%lf", &e);

                }
            //Test to see how far it gets in loop before giving up due to segmentation fault    
            printf("The iteration number is: %d\n", j); 

        }
    printf("This will print if the program reaches the end of the first loop\n");

}

在处理此问题时提供任何提示或指示,我将不胜感激。谢谢!

【问题讨论】:

  • 您在循环中打开文件并且从不关闭它们,并且希望在下一次迭代中失去句柄时关闭它们,为什么您要在一个循环中 fscanf 10000 次循环而不对您读取的值做任何事情?
  • 我没有看到任何文件关闭??
  • 你在什么操作系统/平台上运行它?
  • 我正在使用这些值。它们用于计算,计算不在我上传的代码中,因为这里是因为无论有没有它们,问题都会持续存在。当我运行上面的代码时,我在循环中得到了完全相同的迭代次数。
  • 对不起,我应该说一下操作系统...我在 Windows 8 内的 cygwin64 平台上运行它

标签: c loops for-loop segmentation-fault stack


【解决方案1】:

您需要检查来自fopen() 的返回值。由于您从不关闭输入文件,因此您可能会达到打开文件的限制。然后fopen() 返回NULL,当您尝试将其与fscanf() 一起使用时,您会遇到段错误。

       //Open the files
        FILE *input1 = fopen(input_1, "r");
        if (!input1) {
            printf("open input_1 failed\n");
            exit(1);
        }
        FILE *input2 = fopen(input_2, "r");
        if (!input2) {
            printf("open input_2 failed\n");
            exit(1);
        }
        FILE *input3 = fopen(input_3, "r");
        if (!input3) {
            printf("open input_3 failed\n");
            exit(1);
        }
        FILE *input4 = fopen(input_4, "r");
        if (!input4) {
            printf("open input_4 failed\n");
            exit(1);
        }
        FILE *input5 = fopen(input_5, "r");
        if (!input5) {
            printf("open input_5 failed\n");
            exit(1);
        }

        for (i=1; i < 10000; i++)
            {
                //For a given Z value, read in the corresponding values. Usually these input files have ~20000 values in each so the loop would be set to run until the end of the file
                fscanf(input1, "%lf", &a);
                fscanf(input2, "%lf", &b);
                fscanf(input3, "%lf", &c);
                fscanf(input4, "%lf", &d);
                fscanf(input5, "%lf", &e);

            }
        //Test to see how far it gets in loop before giving up due to segmentation fault    
        printf("The iteration number is: %d\n", j); 

        fclose(input1);
        fclose(input2);
        fclose(input3);
        fclose(input4);
        fclose(input5);

【讨论】:

  • 解决了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多